-   Linux - Software (
-   -   Adding timestamp to a BASH script (

thefox 11-10-2007 10:07 PM

Adding timestamp to a BASH script
I am writing a bash script to ping a website at certain times of the day and I need to know how to either write the ping data to a text file to view later or to echo the time and date upon completion of the ping. I will be running the script constantly and won't be there to start and stop it so I need to automate the process as much as possible. I am running Fedora Core 7. Also what is the command to delay the script. For example to pause it until 1 (one) hour later and then restart the ping writing each ping data to a new text file.

Tinkster 11-10-2007 10:29 PM


And welcome to LQ!

You're best option for that task would be to use cron

As for the content (plus time-stamp of the ping into a file with the
timestamp as the file-name) ...

ping host > `date "+%Y%m%d-%H:%M` 2>&1

If you put that inside a script, make it executable [chmod +x script]
and invoke that from cron (assuming that your distro doesn't replace
the default editor (vi) with something more 'user-friendly') here's
what to do (verbatim) to set it up to run every hour:

crontab -e
0 * * * * * /path/to/script


All times are GMT -5. The time now is 09:58 AM.