LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2007, 05:47 AM   #1
laucian
Member
 
Registered: Oct 2005
Distribution: Ubuntu 9.04
Posts: 124
Blog Entries: 2

Rep: Reputation: 15
writing a shell script for scheduled pinging


hi everybody,

i want to write a shell script which does the following..

-ping an IP address
-break pinging after a few seconds(so that it does not ping forever)
-and run this script scheduled (like every 6 hours)


no hacking reasons i need this one for a printer, which gets disconnected after some idle time.

i have no exprience with shell scripts and linux automated tasks..can you give me starting tips etc..

thanks..
 
Old 11-06-2007, 05:54 AM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
Have a look at the man pages for ping and crontab. As an example for the pinging:
Code:
ping -c 5 -w 10 192.168.1.55
The above will ping 182.168.1.55 with 5 echo requests. As a safeguard, the -w 10 tells ping to about abort after 10 seconds no matter what.

You can put the above command into a cron job that can run on a schedule. Have you used cron before?
 
Old 11-06-2007, 05:58 AM   #3
laucian
Member
 
Registered: Oct 2005
Distribution: Ubuntu 9.04
Posts: 124
Blog Entries: 2

Original Poster
Rep: Reputation: 15
this is cool..
no i didn't use cron before..
i just heard its name
 
Old 11-06-2007, 06:04 AM   #4
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
Add something like this in your crontab and the ping command will be executed every six hours.

0 0,6,12,18 * * * ping -c 5 -w 10 192.168.1.55
 
Old 11-06-2007, 06:05 AM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
Normally, you use the command crontab -e to edit your personal cron jobs. Once you've typed that, you'd get a list of your cron jobs which you can change or add to. For example, to run the ping command at 9am and 1pm from Monday to Friday, you could use:
Code:
0 9,13 * * 1-5 (/bin/ping -c 5 -w 10 192.168.1.55)
Hope that helps.
 
Old 11-06-2007, 06:20 AM   #6
laucian
Member
 
Registered: Oct 2005
Distribution: Ubuntu 9.04
Posts: 124
Blog Entries: 2

Original Poster
Rep: Reputation: 15
so i opened the crontab editor with

Code:
crontab -e
and entered the following line (for everyday, every 6 hours..) and wanted to save the logs on root Desktop (i will search for a better place )

Code:
0 6,12,18 * * * /bin/ping -e 5 -w 10 129.206.120.16 > /root/Desktop/printerPing.log
is it right?

Last edited by laucian; 11-06-2007 at 06:24 AM.
 
Old 11-06-2007, 06:25 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
That looks about right, but it tells me something worrying - you are using your root user as your normal day-to-day user, logging in graphically. This is generally not a good idea - you should create a non-privileged user for day-to-day use, and switch to root to perform only those tasks which require it. From the shell you can use su and/or sudo, and from the GUI you can use gksu / gksudo (comes with gnome desktop), or kdesu (comes with KDE desktop).
 
Old 11-07-2007, 03:08 AM   #8
zebias
LQ Newbie
 
Registered: Nov 2007
Location: Harare Zimbabwe
Distribution: Red Hat 9
Posts: 17

Rep: Reputation: 0
cat crontab -e to view the crontab editor which is where you may save yr scheduled tasks.

/etc/crontab

zebias zingoni
Mweb Zimbabwe
 
Old 11-08-2007, 03:52 AM   #9
laucian
Member
 
Registered: Oct 2005
Distribution: Ubuntu 9.04
Posts: 124
Blog Entries: 2

Original Poster
Rep: Reputation: 15
i am still having problems..i have done the following, but i don't see my mistake..

i have edited /etc/crontab file with vi

and entered

Code:
00 06,12,18 * * * /bin/ping -c 5 mfc-printer > /var/log/printerCheckLog
it did not work..

(i tried with the current time like 47 09 * * * /bin....)

crontab -e did not help either


i checked the script via KDE system guard..it does not even start a ping job..
 
Old 11-08-2007, 04:20 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you add a job in crontab for testing, make sure you add it a minute or two in advance of the job running... cron starts jobs at the beginning of the minute you specify, so if you save the crontab after :00 seconds in that minute, it's too late and the job will not execute.

First thing to check - that your job works when you run it from the command line. i.e. execute this command from the command line as the same user whose crontab you are editing:
Code:
/bin/ping -c 5 mfc-printer > /var/log/printerCheckLog
Maybe that user doesn't have permission to write to the file /var/log/printerCheckLog?

You can also check the local mail for that user. Errors (or in fact any output from cron jobs) is sent there. It depends on the distro because configurations vary, but usually you will need to set up your mail client to read local mail from the mbox file /var/mail/yourusername.

A command-line mail client like mailx can be useful here, or if you want to go hardcore, just read the /var/mail/yourusername file with less and search for the Subject: header to locate each message.
 
Old 11-08-2007, 04:43 AM   #11
laucian
Member
 
Registered: Oct 2005
Distribution: Ubuntu 9.04
Posts: 124
Blog Entries: 2

Original Poster
Rep: Reputation: 15
first thanks for your reply about not using root as a day to day user..i already have another user account. i am using root only for administration tasks.

here is log of what i have done..

Code:
mars:~ # /bin/ping -c 5 mfc-printer > /root/Desktop/PrinterCheckLog
mars:~ # cd /root/Desktop/
mars:~/Desktop # ls Prin*
Printer.desktop  PrinterCheckLog
mars:~/Desktop # vi PrinterCheckLog
i have run the command on terminal, everything was fine. running vi, i was able to read the log.




Code:
mars:~/Desktop # rm PrinterCheckLog
You have mail in /var/spool/mail/root

mars:~/Desktop # date
Thu Nov  8 10:34:22 CET 2007

mars:~/Desktop # crontab -e
crontab: installing new crontab
mars:~/Desktop # crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXgJiVKd installed on Thu Nov  8 10:34:38 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
38 6,10,12,18 * * *     ping -c 5 mfc-printer > /var/log/printerCheckLog


# This file was written by KCron. Copyright (c) 1999, Gary Meyer
# Although KCron supports most crontab formats, use care when editing.
# Note: Lines beginning with "#\" indicates a disabled task.
as i deleted the old log file, there appeared a mail notification (after i started mailx) how can i deactivate it ?
anyway i edited the crontab file..and now at 10:42 there is no log file..
 
Old 11-08-2007, 05:19 AM   #12
laucian
Member
 
Registered: Oct 2005
Distribution: Ubuntu 9.04
Posts: 124
Blog Entries: 2

Original Poster
Rep: Reputation: 15
here is an another question..
do i have the cron deamon running?
Code:
mars:/etc # ps -ef | grep cron
root      3721     1  0 11:15 ?        00:00:00 /usr/sbin/cron
root      4629  4556  0 11:17 pts/3    00:00:00 grep cron
mars:/etc # ps -ef | grep crond
root      4646  4556  0 11:17 pts/3    00:00:00 grep crond
 
Old 11-08-2007, 06:48 AM   #13
chipotphe
Member
 
Registered: Nov 2007
Posts: 62

Rep: Reputation: 15
Have look at this site :

http://www.adminschoice.com/docs/crontab.htm
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help writing a shell script! smoothdogg00 Programming 5 04-26-2006 12:15 PM
[SOLVED] Writing a shell script mattz40 Debian 5 04-13-2005 07:56 PM
shell script writing ust Linux - General 6 01-24-2005 02:06 PM
writing my first shell script speedhead34 Linux - Newbie 4 12-20-2004 01:50 AM
Help writing a pinging bash script dehuszar Linux - Software 3 06-18-2004 03:03 PM


All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration