get system date, format it, pass it as a parameter to a perl script - all in cron job


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get system date, format it, pass it as a parameter to a perl script - all in cron job
# 1  
Old 02-26-2011
get system date, format it, pass it as a parameter to a perl script - all in cron job

I am trying to get the string containing date - in a specfic format actually, although I think that part is inconsequencial -
Code:
1110226^1110226^1110226^1110226^1110226

- through echo or printf or in some other way - created within a cront job and passed as a parameter to a perl script.
Now, I know it would probably be easier to manipulate the date within that perl script, but I would rather not touch it and pass parameters to it instead - this way I don't have to change the script every time I need a different parameter.
Here is how I get that string at command line:
Code:
/bin/date +'1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d'

Here is the cron job
Code:
$ crontab -l
23 00 * * 1-5 [ -f /home/db2inst1/.bash_profile ] && . /home/db2inst1/.bash_profile && \
  /bin/perl /home/db2inst1/scripts/load_01.pl -u xxx -p yyy -i /home/db2inst1/sql/sql_01.sql \
  -args /bin/date +`1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d` -o /home/db2inst1/OUT/whatever.txt > /home/db2inst1/logs/whatever.log 2>&1

Here is what cron says:
Code:
/home/db2inst1/scripts/load_01.pl -u xxx -p yyy -i /home/db2inst1/sql/sql_01.sql -args /bin/date +`1
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/db2inst1>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=db2inst1>
X-Cron-Env: <USER=db2inst1>
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file

It looks like it is not reading % correctly - do I need something like backslashes in front of those or something?

---------- Post updated at 07:12 PM ---------- Previous update was at 07:02 PM ----------

OK, put the back slashes in front of % signs
Here is what cron says:
Code:
/bin/sh: 1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d: command not found

Although it did read the whole thing and invokes perl and everything else - only that parameter was not created correctly.

---------- Post updated at 08:20 PM ---------- Previous update was at 07:12 PM ----------

It took it -
Code:
`/bin/date +1\%y\%m\%d^1\%y\%m\%d^1\%y\%m\%d^1\%y\%m\%d^1\%y\%m\%d`


Last edited by Scott; 02-26-2011 at 10:33 PM.. Reason: Please use code tags.
# 2  
Old 02-27-2011
my word that looks terrible. If you were on my team I'd
have words!

for pity's sake be sensible and put all that in a shell wrapper;
just because you can do very long cron entries doesn't make it right.

for one, if you make a silly mistake on a line it can throw all your
cron jobs.
for two, it makes other people reading the crontab happier

try it and your problem might go away.
# 3  
Old 02-27-2011
Me 100% agree with bigearsbilly !
# 4  
Old 02-28-2011
Points taken

I agree with the points you've made. I just had to get functionality - before elegance. Will do something like this
Code:
#!/bin/bash
. /home/db2inst1/.bash_profile 
exit 0
#!/bin/perl
/bin/perl /home/db2inst1/scripts/load_01.pl -u xxx -p yyy -i /home/db2inst1/sql/sql_01.sql \
  -args /bin/date +`1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d^1%y%m%d` -o /home/db2inst1/OUT/whatever.txt > /home/db2inst1/logs/whatever.log 2>&1
exit 0


Thanks!

Last edited by Franklin52; 02-28-2011 at 03:00 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass date (YYYY-MM-DD) as parameter and get Day

Hi, I have a requirement where I have to pass Date to a script and get the day from it. Ex If parameter is 2015-09-29 The output should be Tuesday. Can you please tell me how to get that? (6 Replies)
Discussion started by: ashwin3086
6 Replies

2. Shell Programming and Scripting

Pass system date and sysdate-7 in script through crontab

Hi , I have to schedule one job in crontab, but with two parameters. 1. Sysdate in YYYYMMDD format 2. Sysdate - 7 in YYYYMMDD format Please suggest how to do that. Thanks in advance. (1 Reply)
Discussion started by: Anupam_Halder
1 Replies

3. Shell Programming and Scripting

Cron Job - date command problem

Hi I have one problem with cron job in Control Panel. I have a log file that is created once a day on another server and I need to transfer it in an exact time to my server so I wrote the cron job for it BUT the problem is in the date command: /filelog-`date +%Y-%m-%d`.tar.gz; The file... (1 Reply)
Discussion started by: bobo_balde
1 Replies

4. UNIX for Dummies Questions & Answers

Cron job not getting yesterday's date

I have this script that gets yesterday's date which runs fine whenever I call it. But when I set this script to run as cron job on 17/9, I noticed that on 18/9, 19/9 it returned the yesterday's date as 2 days' ago date. on 18/9, yesterday's date was returned as 16/9 on 19/9, yesterday's date... (7 Replies)
Discussion started by: viviensiu
7 Replies

5. Programming

Re : Pass parameters from Cron job to Java code

Hello All, Hope all is fine. I am newbie to Unix. I am using Bourne Shell (sh). One of the question I have is that I am trying to read XML file and based on reading that XML file I want to run same java programs at different hours. When I run the Java code, I wanted to pass parameters to my... (1 Reply)
Discussion started by: samshaw
1 Replies

6. UNIX for Dummies Questions & Answers

a cron job needs a perl script to execute

Hello evreyone, this is my first post, and to say i'm new to this is an understatement. I know very little about perl scripts and hope some one can help me. i'm looking to get a script that a cron job can execute. what the script needs to to is 1) connect to a mysql database 2) go to a... (2 Replies)
Discussion started by: Exader
2 Replies

7. HP-UX

How to create a cron job and run in quality system

Hi Experts, I'm a SAP Basis, I have a small doubt would request you please help on this... 1. I wold like to copy files from one system to another system? as per my knowledge "we have to mount the prod filesystem on the quality box and do a copy every day thru crontab script, or do it via... (2 Replies)
Discussion started by: mahantysk
2 Replies

8. UNIX for Advanced & Expert Users

Cron job for Perl script

Although there are many threads on this forum regarding cron, none have specifically answered my question. So hopefully someone can shed some light on what I'm doing wrong.. I have a perl script that I want to run in a cron job. Since I've read that cron doesn't have any environments set, I... (3 Replies)
Discussion started by: man
3 Replies

9. Shell Programming and Scripting

Cron job at system start up

I want to know if there is a way to make a certain set of programs start in order at system startup with cron or something else i dont know about. (3 Replies)
Discussion started by: rcunn87
3 Replies

10. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies
Login or Register to Ask a Question