Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
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
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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