2014-02-06 87 views
0

这是我想用crontab执行的脚本。BASH shell脚本在命令提示符下正常工作,但不能用于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 

在那之后,我editted crontab中,并用crontab的检查-l

*/20 * * * * sh /home/ksl7922/Memory_test/run_process.sh 

该脚本正常工作的命令行,但是,它不能正常使用crontab的工作。

无论如何,shell脚本似乎可以与crontab协同工作,因为生成了'proc.txt'文件,并且'task_reserved.txt'的第一行被删除。

但是,我没有看到任何消息,以及'mlp'进程的结果文件。

因为我不擅长英语,所以恐怕你们不明白我的意图。 无论如何,任何人都可以让我知道如何处理这个?

+0

提供'mlp'过程的绝对路径。 – devnull

+0

什么是'prgep'?你的意思是'pgrep'? – ooga

回答

0
  1. 我的赌注是PATH环境变量设置不正确内cron。插入

    echo $PATH > /tmp/cron-path.txt 
    

    看看它目前有什么价值。也许您需要在脚本中手动将其设置为适当的值。

    这实际上是常见问题

  2. 如果没有您的系统上的任何mail安装了cron从你的脚本转发的错误信息,这是一个好的做法是手动将所有错误消息重定向到您的首选位置。例如。

    #! /bin/bash 
    { 
        date 
    
        prgep mlp > /home/ksl7922/proc.txt 
        ... snip ... 
        fi 
    } &> /tmp/cron-msg.txt 
    
0

你检查的脚本执行权限?该文件应具有可执行权限。

ls -ltr /home/ksl7922/Memory_test/run_process.sh 
chmod 755 /home/ksl7922/Memory_test/run_process.sh 
相关问题