1

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?

toom
  • 217
  • 1
  • 5
  • 12

3 Answers3

0

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

#!/bin/bash
bash -c "python GetCookie.py"
wait

export DISPLAY=:0
Respond=`screen -ls`

if [[ $Respond != *"UploadImage"* ]]; then

    screen -dmS myAction bash -c " cd ~/DirectoryName ; /usr/bin/php7.4 index.php bag 1"

else
    echo "NOPEEEE"
fi

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

screen -dmS myAction bash -c " cd ~/DirectoryName ; /usr/bin/php7.4 index.php bag 1"

to

screen -d -m -S myAction bash -c " cd ~/DirectoryName ; /usr/bin/php7.4 index.php bag 1"

and my problem solved.

Bravo Net
  • 101
  • 1
0

have you tried something like this instead?

#!/bin/sh
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.

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

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