简体   繁体   中英

How to set up a bash script to run in the background anytime the linux ubuntu server is running

I have written up a simple bash script that will copy the newest image from my ip camera into a directory, rename the file and delete the old file. The script loops every 10 seconds.

I want to have this script start running in the background and run continuously all the time that the server is up.

I understand the part about adding a & to the end of the command will cause it to run in the background.

Is init.d the best place to execute this?

I am running ubuntu server.

This sort of thing is normally done by service scripts , which you would find under /etc/init.d . Depending on the version, that might be a "System V init script", or one of the systemd scripts.

A simple service script of the sort you are asking about would start automatically (based on comments in the script's header that tell what run-levels it would use), create a file under /var/run telling what process-id the script uses (to allow killing it), and run the copying in a loop, calling sleep 10 to space the timing as indicated.

A typical service script should implement "start", "stop", "restart" and "status". Not all do, but there is rarely a good reason to not do this.

On my (Debian) system, there is a README file in the directory which is a good introduction to the topic. There are several tutorials available for the topic. Here are a few:

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