I want to run the following shell script (launch.sh) to start/stop a screen session using crontab
!/bin/bash
cd ~/screen
DATE_FORMAT=+%Y-%m-%d:%H:%M:%S
echo --- START ---
date $DATE_FORMAT
if [ -f ./screen.pid ]
then
PID="$(cat ./screen.pid)"
echo Stopping mitmproxy screen PID=$PID
kill $PID
rm ./screen.pid
fi
echo Rotating log files
logrotate -s ./logrotate.status ./logrotate.config
#Starting proxy in reverse mode
screen -S tty-mitmproxy -d -m mitmproxy -p 3333 -R http://localhost:8000 -a ./mitmproxy.log
if [ $? -eq 0 ]
then
PID="$(screen -ls | awk '/\.tty-mitmproxy\t/ {print strtonum(1ドル)}')"
echo Starting mitmproxy screen PID=$PID
echo $PID > ./screen.pid
fi
date $DATE_FORMAT
echo --- END ---
exit 0
If I run it manually through a bash terminal
/home/fernando/screen/launch.sh >> /home/fernando/screen/launch.log 2>&1
It works as expected
cat /home/fernando/screen/launch.log
Output (after executed twice)
--- START ---
2016年05月10日:22:50:32
Rotating log files
Starting mitmproxy screen PID=4897
2016年05月10日:22:50:32
--- END ---
--- START ---
2016年05月10日:22:50:34
Stopping mitmproxy screen PID=4897
Rotating log files
Starting mitmproxy screen PID=4919
2016年05月10日:22:50:34
--- END ---
In this case, I can reattach to my screen session normally through a bash terminal
screen -r
I would like to do the same, but executing the above shell script via crontab
So, I added the following line to my user's crontab
*/10 * * * * /home/fernando/screen/launch.sh >> /home/fernando/screen/launch.log 2>&1
After the scheduled time, I tried to reattach as before through a bash terminal
screen -r
This time, I got the response
There is no screen to be resumed.
Checking my log file I found this weird output (after executed twice by crontab)
--- START ---
2016年05月10日:23:00:01
Rotating log files
Starting mitmproxy screen PID=
2016年05月10日:23:00:02
--- END ---
--- START ---
2016年05月10日:23:10:01
Stopping mitmproxy screen PID=
kill: uso: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... ou kill -l [sigspec]
Rotating log files
Starting mitmproxy screen PID=
2016年05月10日:23:10:01
--- END ---
I've searched syslog too
grep "launch.sh" /var/log/syslog
May 10 23:00:01 fernando-PC CRON[4992]: (fernando) CMD (/home/fernando/screen/launch.sh >> /home/fernando/screen/launch.log 2>&1)
May 10 23:10:01 fernando-PC CRON[5045]: (fernando) CMD (/home/fernando/screen/launch.sh >> /home/fernando/screen/launch.log 2>&1)
What am I doing wrong?
PS.: I'm running on Ubuntu 14.04 LTS and my screen version is 4.01.00devel (GNU) 2-May-06
EDIT:
The problem seems to be related with the following line of my shell script
screen -S tty-mitmproxy -d -m mitmproxy -p 3333 -R http://localhost:8000 -a ./mitmproxy.log
If I change the command executed inside the screen section this way
screen -S tty-top -d -m top
It works!
2 Answers 2
Looks like your problem is that $PID is not defined. Both your "Starting mitmproxy screen PID" and "Stopping mitmproxy screen PID" messages don't have a PID defined. The error from kill doesn't appear the first time launch.sh is executed from cron because the PID file doesn't exist and the "Stopping mitmproxy" section doesn't execute. The second time around, kill is called with an undefined $PID and hence the error.
Does cron know where your screen and awk are? Do you need to use absolute paths in the section where you determine the PID?
-
I changed my script to provide the full path location for both of them, but results remain the same.Fernando Costa– Fernando Costa2016年05月11日 09:50:39 +00:00Commented May 11, 2016 at 9:50
-
I did another test too. When I change mitmproxy for top, for example, my script is executed as expected by CRON. Therefore, I think mitmproxy is not being started correctly or is crashing right after being started. This would explain why awk is being unable to find its PID.Fernando Costa– Fernando Costa2016年05月11日 11:39:52 +00:00Commented May 11, 2016 at 11:39
-
mitmproxylikely relies on something in your environment that cron doesn't have.waywardone– waywardone2016年05月11日 17:51:56 +00:00Commented May 11, 2016 at 17:51
Try wrapping the call to screen with expect. It looks like the only difference between crontab run and terminal run is that you don't have a pty when you run from crontab. expect can emulate a pty.
crontab -eas your user to add the screen job? It worked as expected for me on Linux Mint. Any error messages incronrelated log files? The relevant line in mysyslog:/var/log/syslog:May 10 10:35:01 fooHost CRON[3911]: (fooUser) CMD (/usr/bin/screen -d -m top)