Link to home
Start Free TrialLog in
Avatar of cer113
cer113Flag for Canada

asked on

Running scripts with Cron

Hi

I am practicing with Linux OS (openSUSE11.3) and I would like to get some information how to run script with Cron. For example I have small script under /usr/src directory and it is called script. When I run it it will show me content of the directory.

How would i configure this script for example to run every 15 minutes for example. or for example every day at certain time

Thanks
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

1. make the script executable
chmod +x /usr/src/script

2. setup cron job
crontab -e
*/15 * * * * /usr/src/script > /dev/null 2>&1
or
0,15,30,45 * * * * /usr/src/script > /dev/null 2>&1
(run at every 15 minutes at time 0, 15, 30, 45 each hour)
> for example every day at certain time
for example, 1:35AM everyday
35 1 * * * /usr/src/script > /dev/null 2>&1
The first five fields are
minute  hour  day  month  week

do
man crontab
for more details.
Avatar of cer113

ASKER

Ok i got that part OK

how do I start cron service because right now

ps -af | grep crond
root      2236  1971  0 01:12 pts/2    00:00:00 grep crond


which means cron is not running
#/etc/init.d/crond start

# chkconfig cront on
As root
rccron restart
One correct

# chkconfig crond start (It will start crond service after reboot of your system)
By the way, it seems there are some issue with OpenSuSE 11 crond.
http://fixunix.com/suse/538817-opensuse-11-0-crond-dies.html
http://forums.opensuse.org/english/get-technical-help-here/applications/407585-cronjob-not-longer-working-suse-11-0-a.html

You might want to get the latest patches for your OS and check the system log for some clues since your crond is disappeared, which should start at boot up by default.
Avatar of cer113

ASKER

cron service is started but the script was never run. I cant see output of the script and it is supposed to run every 15 minutes
You can not see output on your screen, If you want to see the output then you have to ridirect it in a file, then you would be able to see it.
Avatar of cer113

ASKER

how do I do redirect to a file
15 * * * * /root/your_script.sh > /your_directory/output.txt-`date '+%m-%d-%Y:%H%M'`

It will redirect output in a new file with date and time.
Avatar of cer113

ASKER

15 * * * * /usr/src/script > /home/noc/output.txt-`date '+%m-%d-%Y:%H%M'`


so this would work

my script is under /usr/src/script where script is name of the script
and redirects to /home/noc/output.txt


Yes, This should work.
Avatar of cer113

ASKER

unfortunately no luck with this. I cant see any output in output.txt file it is empty
Check the logs in

/var/log/cron.log

If it says something.
Avatar of cer113

ASKER

there is no cron.log under /var/log
could you please list the contect of of /var/log/ and paste the output here.

# ls -la /var/log/ | grep -i cron
Avatar of cer113

ASKER

ls -la /var/log/ | grep -i cron shows nothing
i started service with service cron start but nothing
# ps -ef | grep -i cron (Is it showing anything)
Avatar of cer113

ASKER

ps -ef | grep -i cron
root      4512     1  0 05:01 ?        00:00:00 /usr/sbin/cron
root      4946  3244  0 05:33 pts/1    00:00:00 grep -i cron
ASKER CERTIFIED SOLUTION
Avatar of upanwar
upanwar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cer113

ASKER

no it didnt create file at all in tmp

i guess this is very hard task to do so i guess i will have to leave this to ask someone in person
Avatar of cer113

ASKER

still have a question

by running this command it doesnt show my script is running

This way it is just outputting something in file but how do i know my script is running
Avatar of cer113

ASKER

yes your command created a file in tmp