2

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!

3
  • Did you use crontab -e as your user to add the screen job? It worked as expected for me on Linux Mint. Any error messages in cron related log files? The relevant line in my syslog: /var/log/syslog:May 10 10:35:01 fooHost CRON[3911]: (fooUser) CMD (/usr/bin/screen -d -m top) Commented May 10, 2016 at 17:34
  • @waywardone You're right. It works fine. However, I have described here a simplification of my original problem. In reality, I'm executing screen from a shell script. I'll change my question and add more details. Commented May 11, 2016 at 1:24
  • Would be better to post this question to Unix & Linux? Commented May 11, 2016 at 11:21

2 Answers 2

0

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?

answered May 11, 2016 at 5:03
3
  • I changed my script to provide the full path location for both of them, but results remain the same. Commented 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. Commented May 11, 2016 at 11:39
  • mitmproxy likely relies on something in your environment that cron doesn't have. Commented May 11, 2016 at 17:51
0

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.

answered May 11, 2016 at 11:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.