Linux – Crontab start a screen

bashcrongnu-screenlinuxscripting

I started a screen by shell script in crontab, when I entered the screen, the left side became "sh-4.1#". While I runed the script directly, it was nomal with "[root@localhost data]".
What happened?

Shell script:

#!/bin/bash

cd /data
scr_name="test"

pid=`screen -ls | grep -w $scr_name`
if [ -z "$pid" ];
    then 
    screen -dmS $scr_name
fi

And crontab is:

*/1 * * * * /bin/bash -l /data/test.sh

NOTE: it worked in CENTOS5.5, but not in CENTOS6.3, and now I want to run it in CENTOS6.3.

Best Answer

It's because of the environment variables which are not getting used in the cron.

Specifically in your case, the variable is PS1.

Set this variable in your script and it will work.

Something like this:

#!/bin/bash

scr_name="test"

/bin/env

export PS1=hi

pid=`screen -ls | grep -w $scr_name`
if [ -z "$pid" ];
    then
    screen -dmS $scr_name
fi

This will set your prompt to "hi". Just change it to what you want.