我输入以下内容crontab -e
0 0 * * * bitcoind -datadir=/home/pi/bitcoinData -daemon
0 6 * * * bitcoin-cli -datadir=/home/pi/bitcoinData stop
bitcoind -datadir=/home/pi/bitcoinData -daemon
我希望它每天中午 12 点运行,然后bitcoin-cli -datadir=/home/pi/bitcoinData stop
每天早上 6 点运行。但命令不会被执行。我怎样才能解决这个问题?
“cron status”的输出:
pi@raspberrypi:~ $ sudo service cron status
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled)
Active: active (running) since Tue 2016-05-03 20:57:33 BST; 58min ago
Docs: man:cron(8)
Main PID: 5932 (cron)
CGroup: /system.slice/cron.service
└─5932 /usr/sbin/cron -f
raspberrypi CRON[7608]: pam_unix(cron:session): session opened for user root by (uid=0)
raspberrypi CRON[7615]: (root) CMD (bitcoind -datadir=/home/pi/bitcoinData -daemon)
raspberrypi CRON[7608]: pam_unix(cron:session): session closed for user root
raspberrypi cron[5932]: (root) RELOAD (crontabs/root)
pi@raspberrypi:~ $
(按照建议)我添加>> /tmp/bitcoin-cron.log 2>&1
到 cron 中每一行的末尾。日志文件显示/bin/sh: 1: bitcoind: not found
所以我将程序的完整路径添加到 PATH 中,如下所示:
pi@raspberrypi:~/bin $ locate bitcoind
/usr/local/bin/bitcoind
pi@raspberrypi:~/bin $ locate bitcoin-cli
/usr/local/bin/bitcoin-cli
pi@raspberrypi:~/bin $ export PATH=$PATH:/usr/local/bin/bitcoind
pi@raspberrypi:~/bin $ export PATH=$PATH:/usr/local/bin/bitcoin-cli
pi@raspberrypi:~/bin $ echo $PATH
/home/pi/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/local/bin/bitcoind:/usr/local/bin/bitcoin-cli
再次尝试 cron,它在日志文件中生成了第二行,其中还表示/bin/sh: 1: bitcoind: not found
答案1
解决该问题的方法是使用绝对路径名修改 cron 中的条目。由于机器没有 MTA 来发送失败通知,因此添加了 cron 命令日志记录功能,如下所示:
0 0 * * * /usr/local/bin/bitcoind -datadir=/home/pi/bitcoinData -daemon >> ~/bitcoinData/bitcoin-cron.log 2>&1
0 6 * * * /usr/local/bin/bitcoin-cli -datadir=/home/pi/bitcoinData stop >> ~/bitcoinData/bitcoin-cron.log 2>&1