LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-24-2009, 06:35 AM   #1
Lossenelin
Member
 
Registered: Sep 2002
Location: New Zealand
Distribution: Ubuntu 10.10
Posts: 38

Rep: Reputation: 15
Question 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
 
Old 09-24-2009, 06:43 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by Lossenelin View Post
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.
 
Old 09-24-2009, 02:51 PM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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).
 
  


Reply

Tags
cron, scripts, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issues while executing shell scripts through Cron job paragkalra Programming 8 08-02-2008 01:18 PM
Running Scripts at Boot, via Cron etc maxhugen Linux - Newbie 2 12-06-2005 09:42 PM
can echo be used for popups in shell scripts run by cron? dr_zayus69 Linux - Software 4 04-12-2005 06:30 PM
running shell scripts using Java niravk8 Programming 2 02-09-2005 09:42 AM
running shell scripts salparadise Linux - Newbie 2 12-21-2002 05:40 AM


All times are GMT -5. The time now is 02:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration