We all know that cron doesn't use most of your variables, but it was driving me crazy trying to figure out how to get a text file output with a certain column width.
I used at to help me figure out what was going on.
The XS 0.6 doesn't have at installed by default, so first off:
yum install at
Start the service
service atd start
The script I was trying to run from cron used fbcmd to output my Facebook wall to a text file so my Facebook phobic friends can read it from my Apache server so they don't feel left out. Running it manually worked just fine, but the text output from cron was messed up. Specifically, lines weren't wrapping properly.
The myfacebook script is just this:
#!/bin/bash
DATE=`date +%Y-%m-%d`
TIME=`date +%H:%M`
/bin/echo "Last Updated" $DATE $TIME > /var/www/html/private/anna/facebook.txt
/bin/echo "----------------------------------------------------------" >> /var/www/html/private/anna/facebook.txt
/usr/local/bin/fbcmd fstream =me 20 -satt -sd >> /var/www/html/private/anna/facebook.txt
I have a password protected web directory on my server, so that's where I output it. This would be really nice to have in a cron job so my users don't have to nag me to manually update it.
After some Googling, I used at to generate a script with all my environment variables to run from cron.
Here's what I typed in:
at now + 1 hour < ENTER >
/home/anna/bin/myfacebook < ENTER >
< CTRL+D >
And here's what that looks like:
[anna@schoolserver ~]$ at now + 1 hour
at> /home/anna/bin/myfacebook
at> < EOT >
job 9 at 2010-12-17 00:31
As root, I meander over to where at keeps the spooled jobs.
[root@schoolserver ~]# cd /var/spool/at
[root@schoolserver at]# ls
a000090148b787 spool
The a000090148b787 file is the script that anna just generated with at. I move it over to /home/anna/bin as facebookcron and chown anna:anna /home/anna/bin/facebookcron
Well, it didn't make any difference at first. The lines still weren't wrapping. So I look in my mailbox and at sent me a message!
tput: No value for $TERM and no -T specified
Well, that might explain it. In the facebookcron script, I added these two variables amongst the slew that at had already defined for me.
TERM=linux; export TERM
COLUMNS=130; export COLUMNS
And wouldn't you know the fbcmd text output wrapped quite nicely when I ran facebookcron with cron!
I stopped the at service until I need it again.
service atd stop
I want to run it every two hours at a quarter after, so here's the crontab entry.
15 */2 * * * /home/anna/bin/facebookcron >/dev/null 2>&1
And of course my friend kevix looked at everything and did this up right quick, which works great:
#!/bin/bash
T="/var/www/html/private/anna/facebook.txt"
/bin/echo "Last Updated $(date +%Y-%m-%d) $(date +%H:%M)" > $T
/bin/echo "———————————————————-" >> $T
TERM=linux COLUMNS=130 /usr/local/bin/fbcmd fstream =me 20 -satt -sd >> $T
This was one of my references:
http://aplawrence.com/Unixart/cron.html