linux定时任务crontab中service systemctl无效,原因是无环境变量$PATH
使环境变量在crontab中生效的方法:(建议第三种)
1.传参的方式
crontab中
*/1 * * * * /root/test/shell/test_crontab_env.sh “test_crontab_env”
test_crontab_env.sh 中
#!/bin/bash
echo "date +%Y-%m-%d" "%H:%M:%S
$1" >> /tmp/test_crontab_env.file
2.在该shell脚本中定义环境变量
test_crontab_env.sh 中
#!/bin/bash
TEST_CRONTAB_ENV=test_crontab_env
echo "date +%Y-%m-%d" "%H:%M:%S
$TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
3.在该shell脚本中加载环境变量文件
#!/bin/bash
source /etc/profile
echo "date +%Y-%m-%d" "%H:%M:%S
$TEST_CRONTAB_ENV" >> /tmp/test_crontab_env.file
4.
0 2 * * * . /etc/profile; sh /root/test/shell/test_crontab_env.sh
菜鸟教程第一页就有写这个问题
Debian重启crontab
/etc/init.d/cron restart
/etc/init.d/cron stop
/etc/init.d/cron start
service v2ray restart