根本无法使Crontab作业正常工作

时间:2018-08-22 12:27:25

标签: linux bash cron

Crontab的新使用,我试图运行一个简单的bash脚本。

add_temp:

#!/bin/bash

rnd1=$RANDOM
range1=20
let "rnd1 %= $range1"
rnd2=$RANDOM
range2=50
let "rnd2 %= $range2"

echo $rnd1
echo $rnd2

cd /var/www/html

sqlite3 test.db <<EOF

INSERT INTO temps (date, temp) VALUES ($rnd1, $rnd2 );

EOF

crontab -e:

SHELL:/bin/bash

* * * * * /var/www/html/add_temp

这似乎根本没有运行。如果使用/ var / www / html / add_temp手动运行,效果很好。

1 个答案:

答案 0 :(得分:0)

My guess is that the sqlite3 is not found when the cron daemon run the script. You could modify the script and provide the full pathname of this command. Use a terminal to display the full pathname of the command:

type sqlite3

The cron daemon which will run the script does not have the exact same environment than the bash login shell used to run manually the script. The variable that is usually differently set is PATH.

The first step to troubleshoot this situation is to compare the environments and debug the script.

I suggest to insert these lines below, after the very first line of the script

env
set -x

Run the script manually and redirect output, from a terminal:

/var/www/html/add_temp >/var/tmp/output_m.txt 2>&1

Change the crontab line for:

* * * * * /var/www/html/add_temp >/var/tmp/output_c.txt 2>&1

Wait that the cron daemon executes the script and compare the /var/tmp/output_m.txt and /var/tmp/output_c.txt files.

When the script will be fixed, remove the 2 debug lines from the script and restore the crontab original content.