I am working on automating a maintenance routine that involves starting and stopping a script that is running in a screen session. My approach is to kill the screen session, and then restart it and run the command from within a script using the abilities to both create a screen and pass a command without needing to attach to the screen.
However, I am having difficulties with this. I can create the screen correctly without it attaching using screen -d -m -S screen_name
. However, if I run a command based on:
screen -S screen_name-X stuff "command 1"'echo -ne '\015''"command 2"'echo -ne '\015''
with the echo -ne '\015' being wrapped with backticks rather than single quotes. It is to simulate the user pressing the enter key as the commands I use are moving to a directory and executing a script located there. This command works, but only if the screen has been attached to once it has been created. As I am trying to automate the process of creating the screen and running the commands within it I would like to avoid having to attach and detach within a script. I will be trying the suggestion of creating a shell script containing the commands I need to execute within the screen and edit according to my results.
Is there a way to create a screen and run a command within the screen either in one command, or without having to attach to the screen after creating but before execution of the command?
Thanks in advance.
**Update – having tried the suggestion to place the commands I need to execute within a shell script I have been able to successfully create a screen and execute the commands from within the screen, but I am getting the behaviour that when the script stops running the screen closes as well. This shouldnt be a problem as the script is a logging script that should only stop with the knowledge of the sys admin or through the script I am trying to develop, however it would be preferable to have the screen setup in such a way that the screen does not disappear if the script is stopped. Is it possible to achieve this behaviour? **
Best Answer
I think you may be running into several issues.
If the command finishes before you re-attach, the screen will go away. You can demo this by using:
It will run the
ls -l
command, butscreen -list
won't show it as the screen process has ended.I also have no idea what you are trying to do with this \015 stuff. Perhaps updating your question would help, as what I think you're trying to do is run multiple commands in a screen session. This should be as simple as:
If these are used a lot, perhaps you should make a shell script that runs just these commands, then use a more simple: