LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   writing a shell script for scheduled pinging (https://www.linuxquestions.org/questions/linux-newbie-8/writing-a-shell-script-for-scheduled-pinging-597465/)

laucian 11-06-2007 04:47 AM

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

gilead 11-06-2007 04:54 AM

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?

laucian 11-06-2007 04:58 AM

this is cool..
no i didn't use cron before..
i just heard its name

yongitz 11-06-2007 05:04 AM

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

gilead 11-06-2007 05:05 AM

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.

laucian 11-06-2007 05:20 AM

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?

matthewg42 11-06-2007 05:25 AM

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).

zebias 11-07-2007 02:08 AM

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

/etc/crontab

zebias zingoni
Mweb Zimbabwe

laucian 11-08-2007 02:52 AM

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

matthewg42 11-08-2007 03:20 AM

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

laucian 11-08-2007 03:43 AM

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

laucian 11-08-2007 04:19 AM

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


chipotphe 11-08-2007 05:48 AM

Have look at this site :

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


All times are GMT -5. The time now is 01:04 AM.