# Going from Kubuntu to FreeBSD and back (cron)



## dwong (Mar 15, 2011)

Long story short, I have to get my Kubuntu computer to run a script that will access our FreeBSD server, retrieve some login logs from the previous day, and then e-mail them to someone else using my Kubuntu workstation. I'm a raw novice at this, and I'm sure there's a better way, but I can't find it. Here's the crontab on my machine (details hidden to protect the info):


```
0 8 * * * ssh <freebsd server> 'grep "Authentication SUCCESS" <directory>xx.xx.xx.x*/<logname>.log | grep `date -v-1d "+\%Y-\%m-\%d"` | 
cut -d "[" -f 2 | cut -d "]" -f 1 | sort | uniq -c' | mail -s "Yesterday's Success Log" [email]person@place.com[/email]
```

It works when I run it from the command prompt in Konsole (minus the slashes in front of "+\%Y-\%m-\%d"), but when the crontab runs in the morning, the recipient only receives an "=" sign. My much more experienced co-worker and my boss can't figure out why it's not working, either. I can't find anything on the Internet that pertains to this specifically. Any help will be greatly appreciated.


----------



## DutchDaemon (Mar 15, 2011)

Use full paths to all of the commands, or put an extended PATH= declaration at the top of the crontab. Cron has a limited path. Search the forums, it's being asked repeatedly. And: with a command line this long, putting it all in a script and calling that script from cron is probably preferable.


----------



## wblock@ (Mar 16, 2011)

#2 in the The Interrupted Unix FAQ.


----------



## dwong (Mar 16, 2011)

Thank you, wblock. I'll give that a try.


----------



## dwong (Mar 16, 2011)

DutchDaemon: My apologies, I just realised I forgot to thank you as well. Thank you for the help.


----------



## Zare (Mar 17, 2011)

@wblock, that's f*king awesome!


----------



## dwong (Mar 18, 2011)

Okay, I've added paths, made it a shell script, and I setup up a public key for ssh-agent to automatically log in. Unfortunately, It's still e-mailing me blanks. I've checked and the log for the day specified exists.

cron:

```
# m h  dom mon dow   command
#for retrieving success logs and emailing them
0 8 * * * /home/<directory>/Documents/shell/logsuccess
```

Shell script:

```
#!/bin/bash
#for retrieving success logs and emailing them
/usr/bin/ssh me@there '/usr/bin/grep "Authentication SUCCESS" /<directory>/xx.xx.xx.x*/<logname>.log | /usr/bin/grep `/bin/date -v-1d "+\%Y-\%m-
\%d"` | /usr/bin/cut -d "[" -f 2 | /usr/bin/cut -d "]" -f 1 | /usr/bin/sort | /usr/bin/uniq -c' | /usr/bin/mail -s "Yesterday's Success Log" 
me@myemail.com
```

I did try searching other posts before I posted here again, but I can't seem to find anything that I'm able to adapt to the issue (at least, at my current level of expertise).


----------



## wblock@ (Mar 18, 2011)

dwong said:
			
		

> Unfortunately, It's still e-mailing me blanks. I've checked and the log for the day specified exists.



Watch out for bash versus sh.  A server might not have bash installed, and there isn't anything here that needs it anyway.

Using ssh(1) just makes this harder.  Why not add it to the crontab on the server?

Backslash escaping the percent signs in the date value is not needed and making grep think they're backreferences.

Using shell features like line continuations and variables helps.

```
#!/bin/sh
#for mailing success logs

# settings
logfiles="/dir/sooper-secret*/mystery.log"
maildest="me@invalid.invalid"
dateval=`/bin/date -v-1d "+%Y-%m-%d"`

# commands
cut="/usr/bin/cut"
grep="/usr/bin/grep"
mail="/usr/bin/mail"
sort="/usr/bin/sort"
ssh="/usr/bin/ssh"
uniq="/usr/bin/uniq"

$grep "Authentication SUCCESS" $logfiles \
| $grep $dateval \
| $cut -d[ -f2 \
| $cut -d] -f1 \
| $sort \
| $uniq -c
# | $mail -s "Yesterday's Success Log" $maildest
```

That's untested.  Well, untested after I accidentally sent out four emails due to "hidden" settings.  If you have to hide something, use .invalid domains.


----------



## dwong (Mar 18, 2011)

Thanks, wblock. I'll see if I can get that to work.


----------



## wblock@ (Mar 19, 2011)

Additional: quoting filenames helps in case there's an embedded space.  "logfiles" maybe should be called logfilepattern.  And watch out for that single-quote in the last line.


----------

