Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have written the following script to run for when users have last logged in. I am trying to export a cronjob to a .out file. For some reason, I cannot get it to export.

#!/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

I believe the bash was moved to the correct directory /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

Also, when ls -l of the /tmp/ folder, I reiceve the following:

[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

here is my /etc/crontab edit : */10 * * * * Mike /home/Mike/watchuser >> /tmp/watchuser.out

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes, of course. Because you have some mistakes.

First of all, your script should look like:

#!/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

And second, edit the crontab entries using crontab -e command (by default this will edit the current logged-in users crontab) and add the following line:

*/10 * * * * /home/Mike/watchuser >> /tmp/watchuser.out

...It doesn't need your name in front of the path to the script.

More about: http://en.wikipedia.org/wiki/Cron

share|improve this answer
    
if he's using /etc/crontab, the 6th field needs to be the user name under which to run this. –  roadmr Oct 4 '13 at 15:27
1  
@roadmr No, it's not the case. I bet that he is using /tmp/crontab.RJIkgB as you can see from his output of ls -l /tmp/ command. –  Radu Rădeanu Oct 4 '13 at 15:34
    
good catch, then he's wrong when he says (last line) "here is my /etc/crontab" :) –  roadmr Oct 4 '13 at 15:44
    
@roadmr Ok, I edited my answer to be clear. –  Radu Rădeanu Oct 4 '13 at 15:51
    
Ahh, that could be it. It is odd that it shows the year of 1969. Any idea how to change that to the watchuser.out? –  Macenbro Oct 4 '13 at 21:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.