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"
@AlanCurry, Nope, this doesn't work for me either, even though the command runs perfectly (and takes several hours) when run in a screen manually. – Cerin – 2014-09-27T03:08:50.523
is there any updated answer? – Oki Erie Rinaldi – 2016-03-08T13:53:04.097
4no dice. screen -d -m command, screen -list says no sockets, screen -r no sessions – darkfeline – 2012-07-29T02:24:58.367
1Somehow your command wasn't found, or isn't working correctly in the automatically-created screen environment. Try just doing
screen yourcommand
without the-d
and-m
and see how that goes first. – Alan Curry – 2012-07-29T02:28:01.387You're sort of right. screen terminates when the command finishes, contrary to my expectations. But your answer does work. – darkfeline – 2012-07-29T02:34:32.643
4You can change that by enabling the
zombie
option in screen. Putzombie xy
in your~/.screenrc
. It should also be possible to enable it for one session only b puttingzombie xy
in another file and using-c file
but for some reason that's not working when I try it. Or just usesh -c 'yourcommand;while :;do sleep 9999; done'
– Alan Curry – 2012-07-29T02:39:15.400