How can I get screen to execute a command and then detach (That is, automatically in a single script without further input beyond initially starting the script)? e.g. I run myscript.sh and it automatically starts a screen session, executes a command, then detaches.
This is an easy one:
screen -d -m yourcommand
From the Screen User's Manual:
Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.
To run a single command in screen and detach, you may try:
screen -dm sleep 10
To run multiple commands, try:
screen -dm bash -c "sleep 10; myscript.sh"
Please note that when a program terminates, screen (per default) kills the window that contained it.
If you don't want your session to get killed after script is finished, add
exec sh at the end, e.g.:
screen -dm bash -c 'sleep 5; exec sh'
To list all your sessions, try:
Related: Start Unix screen, Run command, Detach.
it happen to me when I pressed control c (sig int) to exit my program. it exits all the way from all bash. so I found this to catch SIGINT. and prevent exit from last bash. (need to type exit to exit)
screen -dmS "screenNameHere" bash -c "trap 'echo gotsigint' INT; cd /mydir ; my_command_here; bash" example: screen -dmS "status_updates" bash -c "trap 'echo gotsigint' INT; cd /opt/status_update ; forever index.js ; bash"
I find it useful to use cron to run nodejs programs on startup. and to run the screen at boot time. in cron there are special events syntax @reboot event
to edit cron, execute: crontab -e then type @reboot screen -dmS "screenNameHere" bash -c "trap 'echo gotsigint' INT; cd /mydir ; my_command_here; bash"
Here are the steps you can follow to run a process in screen, detach from the terminal, and then reattach.
From the command prompt, just run
screen. This will give you a new subshell.
Run your desired program
Detatch from the screen session using the key sequence
Ctrl-a Ctrl-d(note that all
screenkey bindings start with
Ctrl-a). This will drop you back to your original shell and display a message "[detached]", indicating that the screen session is still running.
You can then list the available screen sessions by running
You can reattach to this screen session by running
screen -r. Once reattached, you will be able to take off where you left off and see any output that was printed to the screen during the time that you were detached. If you have multiple screen sessions, then you can specify the tty name (as displayed by
screen -list) as an argument to
screen -rto attach to a particular session.
Buliding on some of the ideas in other answers here, and on https://serverfault.com/questions/368054/run-an-interactive-bash-subshell-with-initial-commands-without-returning-to-the my soloution was.
screen -dm bash -c 'bash --init-file <(echo command)'
command with the command to execute.
This is the closest equivilent I can think of to manually starting a screen session and then running a command inside it.
ctrl+c works as it normally would in an interactive session to kill the program and return to a shell inside screen. Unfortunately ctrl+z doesn't seem to work.