Operating System - Linux
1827603 Members
3410 Online
109966 Solutions
New Discussion

scheduling a shell script

 
SOLVED
Go to solution
Shivkumar
Super Advisor

scheduling a shell script

Dear Sirs,

i want to schedule a shell script jasmine.sh to execute every day at a particular time but don't want to put it in cron job. i want to execute as a login account on mine i.e. as a non-root user.

how can i achieve this ?

thanks,
shiv
22 REPLIES 22
Sivakumar TS
Honored Contributor
Solution

Re: scheduling a shell script



Dear Shiv,

you can add the entry in the crontab, ( in the command field)

su - -c

now, in the sheduled intervals , the script will be executed as the specified in the above command ( and not as root). You can specify your username and your script.

With Regards,

Siva.
Nothing is Impossible !
Pete Randall
Outstanding Contributor

Re: scheduling a shell script

You can use at:

echo "/full_path/script.sh" | at 0200 tomorrow

Just add that line to the end of your script and it will repeat every day.


Pete

Pete
Patrick Wallek
Honored Contributor

Re: scheduling a shell script

You can use cron to schedule jobs for users other than root.

First the user must be allowed to use cron. Add the user to /var/adm/cron/cron.allow

Then the user can edit his/her crontab with the 'crontab -e' command. Or root can edit another users crontab via 'crontab -e username'.

When the jobs run they will be run as that user.
Lolupee
Regular Advisor

Re: scheduling a shell script

Pete has just given the best idea.

The use of echo "/$PATH/filename" | at
Lolupee
Regular Advisor

Re: scheduling a shell script

apologies, you must be permitted to use "at" on the server for this.

let sysadm add your name to at.allow
John Payne_2
Honored Contributor

Re: scheduling a shell script

HP SIM can do this for you. (I am talking SIM 5, not the older revs. I also don't know if you have it installed or are familiar with it.)

I use HP SIM to login and run commands for me regularly, and it has a 'run now' option, and a 'schedule' option.

Short of running cron or some other scheduler, this is likely your best (free) bet. SIM is normally used for monitoring, but I use it for sysadmin tasks much more than monitoring.

Hope it helps

John
Spoon!!!!
Arunvijai_4
Honored Contributor

Re: scheduling a shell script

Hi Shiv,

"at" is the command to help you.

http://docs.hp.com/en/B2355-60127/at.1.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Yogeeraj_1
Honored Contributor

Re: scheduling a shell script

hi,

i usually prefer:

20 09 * * * echo "/prod/myscripts/jasmine.sh"|su - shiv 1>/prod/script/logfiles/output-jasmine.crn 2>/prod/script/logfiles/error-jasmine.crn

using cron of course

hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Senthil Prabu.S_1
Trusted Contributor

Re: scheduling a shell script

Hi shiva,
you can use both "at" and cron jobs to do it.
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Devender Khatana
Honored Contributor

Re: scheduling a shell script

Hi Shiv,

The best way although for executing the repeated jobs at a particular time is cron only. But as you already mentioned of asking the options apart from cron, a loop will be another option.

at will work but it will only process the job only once and for rescheduleing either you reschedule it again or put the rescheduling option is the script itself.

A loop will also achive the same function as you can sleep the loop for a particular time.

The loop file shall contain

#!/bin/sh
while true
do
/path/script
sleep 86400
done

The script should be executed at the right time initially, if not manually possible then use at for first use. Also you will require to restart it at every reboot.

HTH,
Devender
Impossible itself mentions "I m possible"
Arturo Galbiati
Esteemed Contributor

Re: scheduling a shell script

Hi Shiv,
you can sue 'at' command or if you have an Oracle instance you can use the dbms_jobs package.
HTH,
Art
Shivkumar
Super Advisor

Re: scheduling a shell script

Lolupee's idea is good. Devender, is 86400 execute every day at a particular time ?
Shivkumar
Super Advisor

Re: scheduling a shell script

Sorry, it was Pete Randall's idea. Thanks Pete!!
Patrick Wallek
Honored Contributor

Re: scheduling a shell script

The 'sleep 86400' will cause the script to sleep for 86,400 seconds or 24 hours. It will then execute again.
Howard Marshall
Regular Advisor

Re: scheduling a shell script

Be weary of using at to continuously reschedule a job that has to run every day.

All it has to do is fail to finish the script once for any reason and its not scheduled any more. On the same note, all it has to do is get run one extra time manually and suddenly its scheduled twice.

Based on your original request I would recommend using cron for that user instead of the root cron.


H
Devender Khatana
Honored Contributor

Re: scheduling a shell script

Hi Shiv,

sleep 86400 will repeat the process after that manu seconds.

Caution - After every execution the time will be reduced by one second and obviously after 30 days in sequence you will find that the command is actually executing 30 sec. prior to the time set.

HTH,
Devender
Impossible itself mentions "I m possible"
Arunvijai_4
Honored Contributor

Re: scheduling a shell script

Hi Shiv,

sleep 86400 will execute a script after 24 hrs exactly comleting the first run.

http://unix.derkeiler.com/Mailing-Lists/HP-UX-Admin/2004-09/0038.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Shivkumar
Super Advisor

Re: scheduling a shell script

Sir,

I put the below line at the end of the script shiv.sh

echo "/home/sksonkar/shiv.sh"|at 0200 tomorrow

how does the shiv.sh script gets invoked at 2am tomorrow; because the line is at the end of the sript shiv.sh ?

Regards,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: scheduling a shell script

Hi Shiv:

You asked if you do:

# echo "/home/sksonkar/shiv.sh"|at 0200 tomorrow

"How does the shiv.sh script gets invoked at 2am tomorrow?"

This is just another way of specifying the filename that you want 'at' to run. The STDIN file descriptor (usually your terminal) is what is being read by 'at'. You can shorten this to:

# /home/sksonkar/shiv.sh|at 0200 tomorrow

... or write equivalent specifications for starting your script tomorrow (January 22 at 0200):

# at -f /home/sksonkar/shiv.sh 0200 tomorrow

# at -t 01220200 -f /home/sksonkar/shiv.sh

Have a look at the manpages for 'at' for more information.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: scheduling a shell script

Hi James,

I had scheduled the job home/sksonkar/shiv.sh|at 0200 tomorrow

it exexuted successfully today morning.

I believe it will not execute continuously tomorrow and day after.

For making it to run continuoulsy do i need to put the at command in the script shiv.sh and run "shiv.sh &" or run with the command "nohup shiv.sh" as a background process ?

Is there a command to view the details of the jobs scheduled via at command ?

Thanks,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: scheduling a shell script

Hello Shiv:

No, the script will not automatically execute again as setup.

One crude way to make continuous execution happen, as already noted, would be to place the body of your script's instructions in a loop that once executed, sleeps for 86,400 seconds. The drawback, of course, is that if your script ever fails, it's done until you manually intervene.

Another way to cause successive runs would be to use 'at' as you have, and add, as the *last* instruction for your script to re-schedule itself:

# at -f /home/sksonkar/shiv.sh now + 1 days

See the manpages for 'at' for more options!

Lastly, if you want to run a task with regularity, you should 'cron' it. This is what 'cron' is designed to do. You can run a process every day, many times a day, only on selected days of the week, etc. etc.

Please consult the 'crontab' manpages. For example, to run your script every 24-hours you would create a crontab entry (using the user account you want to use) like:

30 2 * * * /home/sksonkar/shiv.sh

This would run your script every day at 02:30

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: scheduling a shell script

Hi (again) Shiv:

Oh, I forgot to answer your last question.

To see tasks (if any) that have been scheduled via 'at', login as the user that scheduled them and do:

# at -l

To remove one, use:

# at -r

...with the job-id seen in 'at -l'. For instance:

# at -l
job 1138035079.a at Mon Jan 23 11:51:19 2006
# at -r 1138035079.a

Since you will ask, (good!) I'll tell you ahead of time that the jobid, here "1138035079" is the Unix epoch time --- the number of seconds since January 1, 1970. The jobid of 'at' is "translated" in the output. That is, "1138035079" is your localtime as shown: Mon Jan 23 11:51:19 2006.

Regards!

...JRF...