Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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
add comment

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

#!/bin/bash

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

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

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.

share|improve this answer
    
I am using inotifywait in the while true to execute another script every time a file is changed. –  trideceth12 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
add comment

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.

share|improve this answer
    
+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
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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