Please have a look at the following simple shell script startCleanup.sh
:
#!/bin/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?
have you tried something like this instead?
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.
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:
i'm facing the similar issue like you, manually trigger the sh file is totally fine. but in cronjob it doesn't execute the screen process, this is my original code of the sh file
the cronjob log did show that it has trigger the job. but in screen -ls doesn't show any on going process.
what i did was change
to
and my problem solved.