Running scripts with Cron

cer113
cer113 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
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)
Top Expert 2011

Commented:
> 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.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
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

Commented:
#/etc/init.d/crond start

# chkconfig cront on
Top Expert 2011

Commented:
As root
rccron restart

Commented:
One correct

# chkconfig crond start (It will start crond service after reboot of your system)
Top Expert 2011

Commented:
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.

Author

Commented:
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

Commented:
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.

Author

Commented:
how do I do redirect to a file

Commented:
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.

Author

Commented:
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


Commented:
Yes, This should work.

Author

Commented:
unfortunately no luck with this. I cant see any output in output.txt file it is empty

Commented:
Check the logs in

/var/log/cron.log

If it says something.

Author

Commented:
there is no cron.log under /var/log

Commented:
could you please list the contect of of /var/log/ and paste the output here.

# ls -la /var/log/ | grep -i cron

Author

Commented:
ls -la /var/log/ | grep -i cron shows nothing
i started service with service cron start but nothing

Commented:
# ps -ef | grep -i cron (Is it showing anything)

Author

Commented:
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
Commented:
As per the above given comment your cron service is started at 5:01.

Could you please write below given test job in cron and check whether it create the file or not.

# crontab -e

Paste below given line and save the file.

5,10,15,20,25,30,35,40,45 * * * * ls -la /etc >> /tmp/ls_output.txt


After some time check the file in /tmp/ls_output.txt and its output.

Author

Commented:
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

Author

Commented:
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

Author

Commented:
yes your command created a file in tmp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial