Passing Parameters to Crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing Parameters to Crontab
# 1  
Old 02-09-2015
Passing Parameters to Crontab

Hello Experts,

I have a requirement to pass some parameters to Linux cron tab.
For ex: My default cron entry looks like this as below:
-------------------------------
Code:
55 10 * * -- /usr/sap/BDD/BO/BOIPS/dataservices/log/XXXX_JOB1/Schedules/Finance_DS/DI__6d709d6a_1234_46b5_8268_3bce90c38a30_74_r_0_w_n_10_55.sh

-------------------------------

However I want to pass ¨system hostname¨ as a parameter as below:
-------------------------------
Code:
55 10 * * 0 Rcmd +i HOSTNAME -- /usr/sap/BDD/BO/BOIPS/dataservices/log/XXXX_JOB1/Schedules/Finance_DS/DI__6d709d6a_1234_46b5_8268_3bce90c38a30_74_r_0_w_n_10_55.sh

-------------------------------

Whenever new cron entry appears, I have to modify it manually, but it is not feasible for me.

Could you guide me how can I achieve this using Shell script?

Regards,
Mahesh

Last edited by Scrutinizer; 02-09-2015 at 05:03 PM.. Reason: CODE tags
# 2  
Old 02-09-2015
Not exactly sure how new cron entries are appearing in your environment, however you can use $(hostname -s) in place of HOSTNAME in your cron entry above.
# 3  
Old 02-09-2015
OR,

Is there any configuration file where I can hardcode my system hostname, so the cronjob will get created everytime as per my expected format? This way I dont need to go with script approach.

Please suggest!

---------- Post updated at 02:54 AM ---------- Previous update was at 02:48 AM ----------

Thanks for replying.

Just to give you a background, we are using cron scheduler to executes our SAP Data Services Jobs. When we Schedule a new job in Data Services, its corresponding cron entry gets created automatically. The problem here is, if we dont specify hostname, then cron job executes twice. So to avoid multiple executions, we are passing system hostname here since this is a virtual server.
# 4  
Old 02-09-2015
Could you explain in more detail what you are trying to achieve. We can then suggest a way to achieve this.

For example:
Do you have multiple cron jobs?
How are these job(s) created?
Do you run them from multiple systems?
Why does the hostname mentioned above need to change?

EDIT: OK so SAP is creating the cron jobs.

Can you expand on how multiple executions are occurring and how specifying the hostname avoids them?

Last edited by Chubler_XL; 02-09-2015 at 05:37 PM..
# 5  
Old 02-10-2015
Why not simply modify the invoked shell script to obtain the appropriate hostname?
This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 02-11-2015
I also thought to modify the invoked script at SAP side, however unfortunately this behaviour is hardcoded with the application, so no way I can modify it.

Only the thing posible is, after scheduling a job in SAP, it generates its corresponding cron job at Linux machine and I am looking for a way to pass ¨hostname¨ as extra parameter.

If we dont provide hostname, job execute twice at sap end but crontab log shows just one occurence. To avoid this issue, our UNIX team has suggested to provide additional parameters to get it working as this is a virtual server. I am not sure about the reason behind this but fortunetly it work for us.

Could you please suggest ?
# 7  
Old 02-11-2015
If i understand you correctly you have the following situation:

- You have several servers and want to deploy crontab entries on them.
- The entries should get the local hostname as parameter.

First, i think you won't need to pass the hostname as a parameter because you could find it out inside the cron-script:

Code:
#! /bin/ksh

typeset chLocalHostname="$(hostname -s)"

[...]

do_whatever "$chLocalHostname"

If you still want (for whatever reason) to put the hostname into the commandline write a deploy-script to create the crontab-entry. I am not sure where the crontabs are located in your Linux, so you might have to change the pathes a bit in the following, but the idea should be clear enough:

Code:
#! /bin/ksh

# protect against multiple runs
if grep -q myshellscript /var/spool/cron/crontabs/root ; then
     print - "Already installed - aborting"
     exit 0
fi

# deploy
scp user@deployhost:/some/where/myshellscript /path/to/myshellscript

print - "55 10 * * 0 /path/to/myshellscript $(hostname -s) 2>/dev/null 1>/dev/null" >> /var/spool/cron/crontabs/root

# tell cron to reread config:

killall -s 1 cron

exit 0

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with passing parameters from a file

Hello Everyone, I have developed a shell script which takes schema id and password as parameter to login into database using sqlplus,runs a query and mails the result. My requirement is that, I dont want to pass userid and password as parameters.Instead,I want to pass say Environment... (4 Replies)
Discussion started by: karthik adiga
4 Replies

2. Shell Programming and Scripting

Passing 2+ parameters to one command

I have a script that uses more than one parameter. It looks like this: for i in `cat /tmp/listofpolicies`; do for x in $(cat /tmp/lst |sed 's/^/\/usr\/openv\/netbackup\/db\/class\//g'); do /usr/openv/netbackup/bin/admincmd/bpplinclude $i -delete -f $x;done;done The problem is that the... (3 Replies)
Discussion started by: newbie2010
3 Replies

3. Shell Programming and Scripting

passing parameters with spaces

we are using following script to execute stored procedue. The problem is run_pmcmd.ksh script is using $* parameter which is not taking in account 'Men Shirt' parameter which includes spaces. 1. Step 1 run_pmcmd.ksh CONVERT_TEST script for run_pmcmd.ksh /u01/$(whoami)/run_pmcmd.ksh... (11 Replies)
Discussion started by: sandy162
11 Replies

4. Shell Programming and Scripting

passing parameters to the script

how can i make a script to run only when parameters are given, if parameters are not given it should through an error , saying "please enter a parameter" for ex: i want a find command to run only when the parameters are given (4 Replies)
Discussion started by: knip
4 Replies

5. UNIX for Dummies Questions & Answers

Passing the parameters through a file

Hi All, I have written a shell script and the script is working fine, I am passing my MIT_ID(NUMBER VALUE) to the shell script from the command prompt and my script is executing as expected. Now I have to pass all the MIT_ID's from a .txt file to the shell script,as I am automating this I... (6 Replies)
Discussion started by: gaur.deepti
6 Replies

6. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

7. Shell Programming and Scripting

passing parameters using awk

Hi, The below script is working fine awk1.sh ======= awk BEGIN { FS="|" } FNR==NR { f1=$2; next } $1 in f1 && $2 =="xx" && $1 == "DAILY_JOB" {print $3} awk -f awk1.sh a.txt b.txt--Its working fine . When passing parameters its not working .Any help it should be appereciated. ... (4 Replies)
Discussion started by: akil
4 Replies

8. Shell Programming and Scripting

passing more than 9 parameters

hi, i am passing around 14 parameters for a script a=$1 b=$2 c=$3 d=$4 e=$5 f=$6 g=$7 h=$8 i=\"${9}\" shift j=\"${1}\" still for j it is displaying the 1st parameter value..how to make it take the 10th parameter (2 Replies)
Discussion started by: dnat
2 Replies

9. Shell Programming and Scripting

Passing parameters through a file

I have created a script "myscript.sh" I need to run this script with diffrent parameters. The parameters are stored in a file myparam.txt. I need to run myscript.sh script for every parameter supplied in myparam.txt. Example: If myparam.txt contains following lines: param1 param2 param3... (3 Replies)
Discussion started by: chawlaaman
3 Replies

10. UNIX for Dummies Questions & Answers

Passing parameters in script

I have 2 scripts: script1 and script2 Script1 passes 4 parameters to script2 as follows #script1 code ... ... script2 $var1 $var2 $var3 $var4 Script2 uses the export command to know to expect these values #script2 export $1 $2 $3 $4 code ... ... The problem that I am having is... (1 Reply)
Discussion started by: eliguy
1 Replies
Login or Register to Ask a Question