简体   繁体   中英

Trigger a bash script at startup to execute periodically

I have made a bash script, let's call it script.sh, which has the next sctructure:

#!/bin/bash while true do do_something() sleep 1800 #seconds done

I want the script to run as a task at startup although there is no user connected to the system. I thought that I could use 'nohup script.sh' but I don't know if I can use it at startup without any user connected. Have anybody some idea?

Look into using /etc/cron.hourly/ for an hourly script. It will run hourly at some interval past the hour. On RHEL, this is defined in /etc/cron.d/0hourly as 1 minute past the hour.

You could then extend this framework for half-hour intervals (1800s = 30 minutes), eg, in /etc/cron.d/1_halfhourly :

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
00,30 * * * * root run-parts /etc/cron.halfhourly

And put your script, or symlink it, in /etc/cron.halfhourly .

Naturally, this could be extended right down to one minute intervals, eg, in /etc/cron.d/2perminute :

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
* * * * * root run-parts /etc/cron.perminute

This would run every script under /etc/cron.perminute each minute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM