I have a script that i want to be constantly running in the background but I don't know where to launch it from

If I put it in .bashrc it will run multiple times (one for each login)

It's an infinite loop that I want running provided a user is logged in, but not more than once for each user.


When bash(1) is run as a login shell (i.e., when the user log in) it runs /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile unless invoked with --no-profile. Starting your script from there should do the trick.

  • This is the right approach. Unfortunately some X window managers don't run a login shell: they just give you an X session and you start ordinary xterms yourself. So it may or may not work for your setup. – alexis Mar 4 '13 at 17:48

Your script should check that there is only one instance. Try something like this:


mkdir ~/.lock_script || exit
trap "rmdir ~/.lock_script" EXIT

while true
    # Do whatever the script does continuously, here....

The trap command takes care of cleaning up on exit, so even if the script is killed or terminated, it will clean up. No process can catch (trap) the TERM signal (Signal 9) so if you kill -9 the script, it will not clean up after itself.

mkdir is an atomic operation as far as the kernel is concerned. If the mkdir does not succeed (Eg if the directory already exists) then the command returns an error, which causes the second half of the command to run - eg exit.

You can read the command as "mkdir or else exit" - it is better explained here: http://initialprogramload.blogspot.com/2008/11/single-script-instance.html

I would also suggest that you should not use while true in this way - try if at all possible to think of another way of doing whatever you want to do.

  • I am using inotifywait in the while true to execute another script every time a file is changed. – jsj Mar 4 '13 at 7:38
  • @trideceth12 have you tried solution that I have posted ? – Rahul Patil Mar 4 '13 at 8:46
  • I found the mkdir + trap suggestions pretty helpful, +1. – zenzelezz Mar 4 '13 at 9:46

You can add one more condition as below :

add in .bashrc

export my_script_pid=myscript.${USER}
[[ ! -f $my_script_pid ]] && { touch $my_script_pid; echo run-script; } || echo "script already running"

then in your script add rm $my_script_pid at the last.

  • +1 Good work around solution. But there should be something in *nix to run a program once per login. – mtk Mar 4 '13 at 7:30

You can run the script and can get the logs from the below command:

nohup /path/to/your/script.sh > /dev/null 2>&1 &

It will also give you the output.

  • hey you are sending output to /dev/null then how will give output ?? – Rahul Patil Mar 4 '13 at 7:19
  • OP don't want this, please read question again then reply carefully – Rahul Patil Mar 4 '13 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.