LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Running shell scripts with cron (http://www.linuxquestions.org/questions/linux-general-1/running-shell-scripts-with-cron-757458/)

Lossenelin 09-24-2009 07:35 AM

Running shell scripts with cron
 
I've been having a bit of trouble running a shell script with cron.
A friend of mine does a community radio show and the station has a live stream but no podcasts, so I've set up a script to record the stream and encode it as an mp3 while I'm away using mplayer and lame -that's what I'm trying to do anyway

Here's the script, but it doesn't seem to run- at least, I don't see any of the files it should be outputing, would they be in the cron.weekly directory (where I have the script) or in my home directory?

#This is a script to record 'The Unnamed Show'

#it will record the show from the live stream, then convert the output #to an MP3

#Finally, it will delete any files no longer required





HOME=/home/byron/



58 12 * fri root mplayer -dumpstream http://202.50.176.139:8000/listen.pls

02 2 * fri root killall mplayer

04 2 * fri root mplayer -ao pcm stream.dump

35 2 * fri root lame audiodump.wav The_Unnamed_Show.mp3

05 3 * fri root chmod 777 The_Unnamed_Show.mp3

30 3 * fri root rm stream.dump

31 3 * fri root rm dumpstream.wav

lutusp 09-24-2009 07:43 AM

Quote:

Originally Posted by Lossenelin (Post 3695381)
I've been having a bit of trouble running a shell script with cron.
A friend of mine does a community radio show and the station has a live stream but no podcasts, so I've set up a script to record the stream and encode it as an mp3 while I'm away using mplayer and lame -that's what I'm trying to do anyway

Here's the script, but it doesn't seem to run- at least, I don't see any of the files it should be outputing, would they be in the cron.weekly directory (where I have the script) or in my home directory?

#This is a script to record 'The Unnamed Show'

#it will record the show from the live stream, then convert the output #to an MP3

#Finally, it will delete any files no longer required





HOME=/home/byron/



58 12 * fri root mplayer -dumpstream http://202.50.176.139:8000/listen.pls

02 2 * fri root killall mplayer

04 2 * fri root mplayer -ao pcm stream.dump

35 2 * fri root lame audiodump.wav The_Unnamed_Show.mp3

05 3 * fri root chmod 777 The_Unnamed_Show.mp3

30 3 * fri root rm stream.dump

31 3 * fri root rm dumpstream.wav

In the cron script, you must provide full paths to all executables. This is because cron scripts don't have the same search path that a user shell has.

Also, you have to provide full paths to the data files. Setting:

Code:

HOME=/home/byron/
Means nothing in a cron script. It doesn't change the active directory or make the target files accessible.

slakmagik 09-24-2009 03:51 PM

I'm also not sure about writing each command in your crontab, which is what that looks like you're doing. The only time-sensitive part seems to be the dumpstream. So you could just have a file with something like

Code:

#!/bin/sh
cd dumpdir # this will hold for the duration of the script
mplayer -dumpstream http://202.50.176.139:8000/listen.pls
sleep 3840 # mplayer may have a flag to set duration
killall mplayer
mplayer -ao pcm stream.dump
lame audiodump.wav The_Unnamed_Show.mp3 # can mplayer pipe directly to lame here?
chmod 777 The_Unnamed_Show.mp3
rm stream.dump dumpstream.wav

and run that file as a cronjob at '58 12 * fri'. Also, you should probably not 'killall mplayer'. If you have multiple users running it, or a user running multiple instances, they'll all get whacked. Maybe 'kill $(pgrep -f 'mplayer -dumpstream http://202.50.176.139:8000/listen.pls'). You might also want to wrap the rm in a test to make sure The_Unnamed_Show.mp3 exists/was successfully created. If it wasn't, you can manually try again rather than having missed the show.

Also, while it may not be recommended, I run my cron scripts through a wrapper which then runs the scripts. This gives me the shell and environment I want. This is "Dillon's cron" on Slackware - other distros seem to use really weird crons. FWIW, the essentials of it are simply:

Code:

#!/bin/bash

export JSYSPREFIX="$HOME/sys"
export JBINDIR="$JSYSPREFIX/bin"
export JSCRIPTDIR="$JSYSPREFIX/sbin"
export JVARDIR="$JSYSPREFIX/var"

export PATH=$JSCRIPTDIR:$JBINDIR:/usr/local/bin:/usr/bin:/bin

if [[ $DEBUG ]]; then
    logfile=/dev/stderr
else
    logfile="$JVARDIR/log/cron.log"
fi

"$@" >> $logfile 2>&1

if (( $? == 0 )); then
    echo "`date`: cronwrap ran \"$@\"" >> $logfile
else
    echo "`date`: cronwrap failed to run \"$@\"" >> $logfile
fi

Then my crontab just takes the form of

Code:

10 01 * * * /home/j/sys/sbin/cronwrap foo
10 02 * * * /home/j/sys/sbin/cronwrap bar
10 03 * * * /home/j/sys/sbin/cronwrap baz

This may be really stupid in some way I'm not aware of and it's certainly not bulletproof, but 50-mile long cron lines running stuff as /bin/sh got to bugging me and it mostly Works For Me(tm).


All times are GMT -5. The time now is 06:30 AM.