0

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.

  • 1
    The bash prompt is usually set in ~.bashrc, which is read for non login shells. Try "/bin/bash /data/test.sh" (without -l) – Tim Haegele Jan 8 '13 at 7:26
  • I try it, but it doesn't work – Brighter Jan 8 '13 at 8:49
2

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.

  • Yeah, the PS1 change what I want, but in the screen, some commands not found, like "ll" – Brighter Jan 8 '13 at 8:43
  • That's because it's not reading your profile files where you have setup ll to be alias of ls -l. Simply, read the files where you have setup the alias in your script like source .bash_profile and it will work. – GeekRide Jan 8 '13 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.