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?

Was it helpful?

Solution

  1. My bet is the PATH environment variable is not correctly set within cron. Insert

    echo $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

  2. If you don't have any mail installations on your system for cron 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
    

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
scroll top