0

Just curious how I can make a bash script run not only when I call it but all the time in the background.

I have a bash script that is supposed to look for files and place them in the right folders, and I just want it so i don't have to keep running that script manually. I just want it running in the background and continually will place the files into the right folders as more files are added.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
ksuzy31
  • 31
  • You can background any command from a bash shell like so: $ myscript & – Red Cricket Nov 07 '17 at 20:51
  • 2
    That's not a script, that's a daemon. Please clarify exactly what it is that you are trying to accomplish. – DopeGhoti Nov 07 '17 at 20:51
  • Hi So i a bash script that is supposed to look for files and place them in the right folders and I just want it so i don't have to keep runing that script to make it run. I just want it running in the background and continually will place the files into the right folders as more files are added – ksuzy31 Nov 07 '17 at 20:53
  • To answer the question as asked, you start it as a service / daemon, or simply start it in the background, depending on what exactly "all the time" means. That it is a script is not important; scripts are executables like any other executable. However, you may want to consider carefully what "continually move files" means, and how to do that safely. – AlexP Nov 07 '17 at 21:10
  • You could just create a script with a never ending for/while loop and have it run from ~/.bash_profile whenever you log in. – Time4Tea Nov 07 '17 at 21:20
  • Hi I'm reading about nohup so it can be constantly be running but i keep getting an error message saying "nohup: failed to run command 'program.sh': No such file or directory" – ksuzy31 Nov 13 '17 at 20:09

1 Answers1

4

you can daemonize it as said earlier. Even you can enclose the whole script in a

while true; do
 script comes here
 sleep 300 #that would mean running the actual script every 5 mins
done

be carefull though with that approach and make sure to add logic to start the script upon reboot. People tend to forget what they'd run if you're happy with that you can even run the script via crontab -e

*/5 * * * * yourscript.sh

cron has the advantage of coping well with reboots ;)