Operating System - HP-UX
1827876 Members
2056 Online
109969 Solutions
New Discussion

Setting & Using Variables in 'cron'

 
SOLVED
Go to solution
Greg Satchfield_1
New Member

Setting & Using Variables in 'cron'

Hi,

I would like to use output from the `date` command as part of a file name within the cron. I've tried about every variation I can think of and nothing is working.

Here are examples of what I'm trying to do:
* * * * * DATE=`date +%x`;/tmp/script.sh > /tmp/script_output.$DATE

or, more directly:
* * * * * /tmp/script.sh > /tmp/script_output.`date +%x`

I've tried enclosing the entire command in quotes (both " and '), parantheses, braces, etc... and nothing is working. I'm pretty sure I've done this on other versions of UNIX so maybe it's just HP's implementation of UNIX that doesn't like this?

Any help will be greatly appreciated!

Thanks,
Greg
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: Setting & Using Variables in 'cron'

Greg,

Set your DATE in a script invoked from cron that then fires off your /tmp/script.sh.


Pete

Pete
Mel Burslan
Honored Contributor

Re: Setting & Using Variables in 'cron'

in crontab, change this line to:

* * * * * /tmp/launcher.sh

create the /tmp/launcher.sh as follows

#!/sbin/sh
DATE=`date +%x`
/tmp/script.sh > /tmp/script_output.$DATE

save and exit

chmod 755 /tmp/launcher.sh /tmp/script.sh
(or permissions of your choice, just make sure they are executable by the owner)

HTH

________________________________
UNIX because I majored in cryptology...
Alan Meyer_4
Respected Contributor

Re: Setting & Using Variables in 'cron'

To build upon this launcher idea. You could make it a muylti use launcher and feed it the actual script to run as a parameter so you could use it to launch multiple scripts.

* * * * * /tmp/launcher.sh /tmp/script.sh

create the /tmp/launcher.sh as follows

#!/sbin/sh

SCRIPT2RUN=$1

DATE=`date +%x`
$SCRIPT2RUN > $SCRIPT@RUN.output.$DATE
" I may not be certified, but I am certifiable... "
Alan Meyer_4
Respected Contributor

Re: Setting & Using Variables in 'cron'

the last line should be

$SCRIPT2RUN > $SCRIPT2RUN.output.$DATE
" I may not be certified, but I am certifiable... "
Greg Satchfield_1
New Member

Re: Setting & Using Variables in 'cron'

Thanks everyone for the input. There's obviously many different ways to do this from within a script (you could also put everything within braces and then direct the output to the file thereby keeping it within the same script) but I was hoping to find something directly from within cron to lessen the overhead (i.e., script calling a script) and for maintenance (i.e., ability to see more of what's happening from within cron itself).

I appreciate everyone's feedback.

Greg
Patrick Wallek
Honored Contributor

Re: Setting & Using Variables in 'cron'

This should work:

* * * * * /tmp/script.sh > /tmp/script_output.$(/usr/bin/date +%m%d%y)
Greg Satchfield_1
New Member

Re: Setting & Using Variables in 'cron'

Hey Patrick,

Have you been able to get that to work on an HP-UX 11.11 server? I even tried a direct cut-n-paste and I still only get: "/tmp/script_output." with no date. It's along the lines of what I'm looking for though - all done within cron.

Thanks for the help Patrick,
Greg
Marlou Everson
Trusted Contributor
Solution

Re: Setting & Using Variables in 'cron'

Greg,

I do it all the time.

18 7 * * 1-5 /home/sys/daily > /tmp/daily_output.$(date +\%y\%m\%d)" 2>&1

You have to escape the % with the backslash. Check the man page for crontab(1).

"The sixth field, command (the balance of a line including blanks in a crontab file), is a string that is executed by the shell at the
specified times. A percent character (%) in this field (unless escaped by a backslash (\)) is translated to a newline character, dividing the field into "lines". Only the first "line" (up to a % or end-of-line) of the command field is executed by the shell. Any other "lines" are made available to the command as standard input."

Marlou
Greg Satchfield_1
New Member

Re: Setting & Using Variables in 'cron'

THAT'S IT!!

Thanks for your help Marlou - it's greatly appreciated!!

Greg