1

I have this bash script that start the python script ms.py What is the problem here ?

#!/bin/bash
if [ $(ps aux | grep -e 'ms.py$' | grep -v grep | wc -l | tr -s "\n") -eq 0 ]; 
then python /root/folder/ms.py &
fi

and this in my crontab

*/1 * * * * /root/folder/script.sh

When I start the script manually it works normal.

4
  • 2
    I would make sure your cron user has the necessary programs in the PATH, starting with Python. Perhaps /full/path/to/python /root/folder/ms.py Commented Dec 28, 2013 at 17:45
  • I would check the permissions Commented Dec 28, 2013 at 17:55
  • I'm trying to do this with putty. Permissions are ok. Commented Dec 28, 2013 at 17:59
  • 1
    What does your cron debug output says? By default it should email to root@localhost? Commented Dec 28, 2013 at 20:29

1 Answer 1

1

you are testing the output of that pipeline against the number zero. I assume you want to start your python program only if it's not already running:

pid=$(pgrep -f 'ms.py$')
if [[ $pid ]] && kill -0 $pid; then
    echo already running
else
    echo not running
    python /root/folder/ms.py &
fi
Sign up to request clarification or add additional context in comments.

2 Comments

[[ $pid ]] should be [[ "$pid" ]]. The former won't work if pgrep doesn't match -- it will become [[ ]], which is a syntax error.
@nandhp, not in bash: the double brackets are smart enough to avoid that trap: x=""; [[ $x ]] && echo y || echo n

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.