Please have a look at the following simple shell script startCleanup.sh:

screen -d -m -S cleaner /usr/bin/python3 /opt/cleanup.py 

My crontab looks like this:

* * * * * /opt/startCleanup.sh > /opt/cleanup.log 2>&1

When I run this script in the shell as a nomrally logged in user I do not have any problems. However, if I run this as a regular cronjob, no screen is started. Or better said: The screen is started (I think) but when the script finishes and along with the script the screen is also terminated.

Does anybody have an idea how to solve this problem?

thanks :)

UPDATE: Okay, I now defintely know that the screen is properly executed (I checked that by adding a sleep at the end of the bash script).

So here is what happens: The screen is created. BUT when the bash script finishes the screen is also terminated. So, the screen still depends on the script.

How can I avoid that the screen is terminated when the script finishes?

  • 1
    What do you need the screen for anyway? – Gerald Schneider Sep 12 '18 at 11:31

have you tried something like this instead?

screen -D -R cleaner /usr/bin/python3 /opt/cleanup.py

This combination of parameters will try to reattach to a session if exists, or create if it doesn't, according to the manpage.

Also, I don't think there will be any output redirected to the file, apart any errors occuring when calling screen.

  • Thanks, but this is not what I am looking for. I do not intend to reattach anything. – toom Sep 12 '18 at 11:39

indeed using screen here doesn't seem to have much sense. But I've checked your solution on my box and it works fine. Screen session is started from script started from cronjob and I can attach to it. Try few things:

  1. add full path to screen in startCleanup.sh script
  2. watch /var/log/cron if jobs are started at all
  3. redirect output from /usr/bin/python3 /opt/cleanup.py to file and check if it runs fine when started through cron
  • Thanks, but this doesn't solve the problem. As I said, running the script in a normal shell causes no problems. Therefore the actual command should be okay. I think cron requires some additional things. Also, /var/log/cron does not exist on my machine (Ubunutu 18.04) – toom Sep 12 '18 at 12:33
  • when firing from shell there's PATH variable set so bash can find 'screen' executable. It's not always the case when firing from cron. That's why better is to put full path to 'screen' instead of just 'screen'. As for log check in /var/log/messages or /var/log/syslog, depends on your distro – Krzysztof Księżyk Sep 12 '18 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.