How to Set a Crontab to Execute Commands on a Seconds Time Interval on GNU / Linux and Freebsd

How to Set a Crontab to Execute Commands on a Seconds Time Interval on GNU / Linux and Freebsd

Walking in Light with Christ - Faith, Computing, Diary Articles & tips and tricks on GNU/Linux, FreeBSD, Windows, mobile phone articles, religious related texts http://www.pc-freak.net/blog How to set a crontab to execute commands on a seconds time interval on GNU / Linux and FreeBSD Author : admin Have you ever been in need to execute some commands scheduled via a crontab, every let's say 5 seconds?, naturally this is not possible with crontab, however adding a small shell script to loop and execute a command or commands every 5 seconds and setting it up to execute once in a minute through crontab makes this possible. Here is an example shell script that does execute commands every 5 seconds: #!/bin/bash command1_to_exec='/bin/ls'; command2_to_exec='/bin/pwd'; for i in $(echo 1 2 3 4 5 6 7 8 9 10 11); do sleep 5; $command1_to_exec; $command2_to_exec; done This script will issue a sleep every 5 seconds and execute the two commands defined as $command1_to_exec and $command2_to_exec Copy paste the script to a file or fetch exec_every_5_secs_cmds.sh from here The script can easily be modified to execute on any seconds interval delay, the record to put on cron to use with this script should look something like: # echo '* * * * * /path/to/exec_every_5_secs_cmds.sh' | crontab - 1 / 2 Walking in Light with Christ - Faith, Computing, Diary Articles & tips and tricks on GNU/Linux, FreeBSD, Windows, mobile phone articles, religious related texts http://www.pc-freak.net/blog Where of course /path/to/exec_every_5_secs_cmds.sh needs to be modified to a proper script name and path location. Another way to do the on a number of seconds program / command schedule without using cron at all is setting up an endless loop to run/refresh via /etc/inittab with a number of predefined commands inside. An example endless loop script to run via inittab would look something like: while [ 1 ]; do /bin/ls sleep 5; done To run the above sample never ending script using inittab, one needs to add to the end of inittab, some line like: mine:234:respawn:/path/to/script_name.sh A quick way to add the line from consone would be with echo: echo 'mine:234:respawn:/path/to/script' >> /etc/inittab Of course the proper paths, should be put in: Then to load up the newly added inittab line, inittab needs to be reloaded with cmd: # init q I've also red, some other methods suggested to run programs on a periodic seconds basis using just cron, what I found in stackoverflow.com's as a thread proposed as a solution is: * * * * * /foo/bar/your_script * * * * * sleep 15; /foo/bar/your_script * * * * * sleep 30; /foo/bar/your_script * * * * * sleep 45; /foo/bar/your_script One guy, even suggested a shorted way with cron: 0/15 * * * * * /path/to/my/script 2 / 2 Powered by TCPDF (www.tcpdf.org).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us