My bet is the
PATH
environment variable is not correctly set withincron
. Insertecho $PATH > /tmp/cron-path.txt
to see what value it currently has. Perhaps you need to manually set it to a proper value within your script.
This is actually FAQ
If you don't have any
mail
installations on your system forcron
to forward error messages from your script, it's a good practice to manually redirect all error messages to your preferred location. Eg.#! /bin/bash { date prgep mlp > /home/ksl7922/proc.txt ... snip ... fi } &> /tmp/cron-msg.txt
BASH shell script works properly at command prompt but doesn't work with crontab
Question
Here is the script that I want to execute with crontab.
#!/bin/bash
# File of the path is /home/ksl7922/Memory_test/run_process.sh
# 'mlp' is the name of the process, and 'ksl7922' is my user account.
prgep mlp > /home/ksl7922/proc.txt
# This line will give the number of the process of 'mlp'
result=`sed -n '$=' /home/ksl7922/proc.txt`
echo "result = ${result}"
# if 'mlp' processes run less than six, than read text file one line and delete
# it, and execute this line.
if ((result < 6)); then
filename="/home/ksl7922/Memory_test/task_reserved.txt"
cat $filename | while read LINE
do
# Delete line first.
sed -i "$LINE/d" $filename
# Execute this line
eval $LINE
break;
done
else
echo "You're doing great."
fi
After that, I editted crontab and checked with crontab -l
*/20 * * * * sh /home/ksl7922/Memory_test/run_process.sh
This scripts works properly from command line, however, it doesn't work properly with crontab.
It seems like shell script works with crontab anyway, because 'proc.txt' file was generated, and the first line of 'task_reserved.txt' is removed.
However, I didn't see any messages, and result file of 'mlp' processes.
Since I'm not good at English, so I'm afraid that you guys don't understand my intention. Anyway, can anyone let me know how to handle this?
Solution
OTHER TIPS
Have you checked the execute permission for the script? The file should have executable permission.
ls -ltr /home/ksl7922/Memory_test/run_process.sh
chmod 755 /home/ksl7922/Memory_test/run_process.sh
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow