How to prevent "screen -dmS sessionName program.sh" from disappearing after program.sh finishes?

StackOverflow https://stackoverflow.com/questions/19960357

  •  30-07-2022
  •  | 
  •  

Question

If I run rsync inside screen, then after a while I can login to the server and attach the detached screen.

Now I want to run rsync from crontab. I added this entry to crontab:

03 19 * * *     root /usr/bin/screen -dmS rsyncSess uptime

After crontab executes the command, I run screen -ls but nothing is listed.

After I run /usr/bin/screen -dmS rsyncSess watch -n 5 uptime I can see that session when run screen -ls.

How can I run a script in screen from crontab and then attach that screen session to see the output of rsync (even after rsync is finished)?

Until now I found this tread.

Was it helpful?

Solution

Make sure you:

  • run screen -ls as the same user that is used by crontab to start your command
  • use a main command that doesn't end before you reattach, since that will end your session as well. If you run a short command and want the session to persist, use something like bash -c 'short_command; exec bash' (or, as pointed out in the link you mention, add defzombie ZZ to your .screenrc).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top