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
LinuxUnix OS

Avatar of undefined
Last Comment
cer113
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)
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

> 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 upanwar
upanwar
Flag of India image

Avatar of cer113
cer113
Flag of Canada image

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
Avatar of upanwar
upanwar
Flag of India image

#/etc/init.d/crond start

# chkconfig cront on
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

As root
rccron restart
Avatar of upanwar
upanwar
Flag of India image

One correct

# chkconfig crond start (It will start crond service after reboot of your system)
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

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
cer113
Flag of Canada image

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
Avatar of upanwar
upanwar
Flag of India image

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
cer113
Flag of Canada image

ASKER

how do I do redirect to a file
Avatar of upanwar
upanwar
Flag of India image

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
cer113
Flag of Canada image

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


Avatar of upanwar
upanwar
Flag of India image

Yes, This should work.
Avatar of cer113
cer113
Flag of Canada image

ASKER

unfortunately no luck with this. I cant see any output in output.txt file it is empty
Avatar of upanwar
upanwar
Flag of India image

Check the logs in

/var/log/cron.log

If it says something.
Avatar of cer113
cer113
Flag of Canada image

ASKER

there is no cron.log under /var/log
Avatar of upanwar
upanwar
Flag of India image

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
cer113
Flag of Canada image

ASKER

ls -la /var/log/ | grep -i cron shows nothing
i started service with service cron start but nothing
Avatar of upanwar
upanwar
Flag of India image

# ps -ef | grep -i cron (Is it showing anything)
Avatar of cer113
cer113
Flag of Canada image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of cer113
cer113
Flag of Canada image

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
cer113
Flag of Canada image

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
cer113
Flag of Canada image

ASKER

yes your command created a file in tmp
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo