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.
glenn jackman
249k42 gold badges233 silver badges363 bronze badges
asked Dec 28, 2013 at 17:40
Mycha
1434 gold badges5 silver badges12 bronze badges
1 Answer 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
answered Dec 28, 2013 at 23:18
glenn jackman
249k42 gold badges233 silver badges363 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
nandhp
[[ $pid ]] should be [[ "$pid" ]]. The former won't work if pgrep doesn't match -- it will become [[ ]], which is a syntax error.glenn jackman
@nandhp, not in bash: the double brackets are smart enough to avoid that trap:
x=""; [[ $x ]] && echo y || echo nlang-bash
/full/path/to/python /root/folder/ms.py