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.. |
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 You can put the above command into a cron job that can run on a schedule. Have you used cron before? |
this is cool..
no i didn't use cron before.. i just heard its name |
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 |
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) |
so i opened the crontab editor with
Code:
crontab -e Code:
0 6,12,18 * * * /bin/ping -e 5 -w 10 129.206.120.16 > /root/Desktop/printerPing.log |
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).
|
cat crontab -e to view the crontab editor which is where you may save yr scheduled tasks.
/etc/crontab zebias zingoni Mweb Zimbabwe |
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 (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.. |
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 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: |
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 Code:
mars:~/Desktop # rm PrinterCheckLog anyway i edited the crontab file..and now at 10:42 there is no log file.. |
here is an another question..
do i have the cron deamon running? Code:
mars:/etc # ps -ef | grep cron |
|
All times are GMT -5. The time now is 01:04 AM. |