这是关于 cron 的另一个查询。我已参考本网站上的指南在发布之前,但我仍然无法让这个脚本工作。它旨在在某些网络端口上的活动停止时挂起 PC。该脚本在正常 shell 中按列出方式完美运行。我将环境变量传输到 crontab,如下所示。
如果这是一个显而易见的问题,我很抱歉,但我似乎无法弄清楚。我见过的指南建议将环境变量放在 crontab 中。
# set shell to bash
SHELL=/bin/bash
# copy PATH from bash
PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
# set up gt-netscan variables in here instead of /etc/environment
STDBY_ON=1
STDBY_CT=0
STDBY_LIM=5
01 * * * * /usr/local/include/gt-netscan
脚本如下:
#!/bin/bash
if [[ "STDBY_ON" -eq 1 ]]; then
packets=0
interface="eth0" #interface to listen on
packets=$(timeout 5 sudo tcpdump -c 100 -n -i $interface port 22 | grep -c 192.168.7.4)
if [[ "$packets" -eq 0 ]]; then
let "STDBY_CT+=1"
echo "No packets, loop $STDBY_CT count" >> /var/log/gt-netscan
else
let "STDBY_CT=0"
echo "Loop reset: $packets packets detected" >> /var/log/gt-netscan
echo $(date) >> /var/log/gt-netscan
fi
if [[ "$STDBY_CT" -gt "$STDBY_LIM" ]]; then
echo "Suspending, no activity for 20 minutes on $interface." >> /var/log/gt-netscan
echo $(date) >> /var/log/gt-netscan
let "STDBY_CT=0"
sudo pm-suspend
fi
fi
exit
任何帮助将不胜感激。
编辑 1:根据要求,这是从 var/log/syslog 中 grep 的脚本名称(gt-netscan)的输出。
Jan 31 11:35:15 big-boss kernel: [ 9.231242] init: gt-netscan main process (814) terminated with status 2
Jan 31 11:35:19 big-boss kernel: [ 13.188657] init: gt-netscan main process (2718) terminated with status 2
编辑 2:来自'stat /usr/local/include/gt-netscan'的输出
File: `/usr/local/include/gt-netscan'
Size: 995 Blocks: 8 IO Block: 4096 regular file
Device: 811h/2065d Inode: 664072 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-01-31 12:01:01.979605807 +0000
Modify: 2015-01-27 23:32:07.027119674 +0000
Change: 2015-01-27 23:32:07.035119674 +0000
Birth: -
答案1
看来您在 cron 中没有用户。
# m h dom mon dow user command
01 * * * * {insert user here} /usr/local/include/gt-netscan