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.

Khaled
  • 35,688
  • 8
  • 69
  • 98
Brighter
  • 3
  • 2

1 Answers1

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.

Napster_X
  • 3,333
  • 16
  • 20
  • Yeah, the PS1 change what I want, but in the screen, some commands not found, like "ll" – Brighter Jan 08 '13 at 08: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. – Napster_X Jan 08 '13 at 09:33