我编写了以下脚本,用于在用户上次登录时运行。我试图将 cronjob 导出到 .out 文件。出于某种原因,我无法将其导出。
#!/bin/bash
for User in $(cd /home;ls --hide=lost+found); do
echo "$User online since:"
who | awk '{ print "\tUser: "$1" - Terminal: "$2" - "$3" "$4}'
NOW=$(date +%s -d "Jan 1, 1980 00:00:01")
USR=$(lastlog |awk ' {print $1, ":",$5,$6,$9 } ' | grep $User)
USRDATE=$(echo $USR | cut -d ":" -f2)
(( USRDATE = $(date --date "$USRDATE" +%s) / 86400 ))
(( NOW = NOW/86400 ))
(( DAYS = NOW - USRDATE ))
done
FILE="/home/Mike/watchuser"
OUTPUTFILE="/tmp/watchuser.out"
if [ "$1" == 'out' ]
then
FILE = $OUTPUTFILE
fi
done
我相信 bash 已被移动到正确的目录 /home/Mike/watchuser。
[root@ip-10-245-82-234 ec2-user]# ls -l /home/Mike/watchuser
-rwxr--r--. 1 Mike student 564 Oct 3 20:50 /home/Mike/watchuser
另外,当对 /tmp/ 文件夹执行 ls -l 时,我收到以下内容:
[root@ip-10-245-82-234 ec2-user]# ls -l /tmp/
total 4
-rw-------. 1 Mike Mike 34 Dec 31 1969 crontab.RJIkgB
-rw-r--r--. 1 root root 0 Oct 3 21:57 watchuser.out
这是我的 /etc/crontab 编辑:*/10 * * * * Mike /home/Mike/watchuser >> /tmp/watchuser.out
答案1
是的,当然。因为你犯了一些错误。
首先,你的脚本看起来应该是这样的:
#!/bin/bash
for User in $(cd /home;ls --hide=lost+found); do
echo "$User online since:"
who | awk '{ print "\tUser: "$1" - Terminal: "$2" - "$3" "$4}'
NOW=$(date +%s -d "Jan 1, 1980 00:00:01")
USR=$(lastlog |awk ' {print $1, ":",$5,$6,$9 } ' | grep $User)
USRDATE=$(echo $USR | cut -d ":" -f2)
(( USRDATE = $(date --date "$USRDATE" +%s) / 86400 ))
(( NOW = NOW/86400 ))
(( DAYS = NOW - USRDATE ))
done
FILE="/home/radu/watchuser"
OUTPUTFILE="/tmp/watchuser.out"
if [ "$1" == 'out' ]
then
FILE=$OUTPUTFILE # you had an error here
fi # and another one error here
其次,使用命令编辑 crontab 条目crontab -e
(默认情况下,这将编辑当前登录用户的 crontab)并添加以下行:
*/10 * * * * /home/Mike/watchuser >> /tmp/watchuser.out
...不需要你的名字在脚本路径前面。