2

I wrote a service script to run a specific software at boot time. How can I restrict this software so that it does not run manually again?

I mean software has to have just one instance in the memory..

I don't want to change the source code of my software, I want init to this job for me.

  • 1
    You really should add some locking in the ELF executable (i.e. change the C source code of it). – Basile Starynkevitch Aug 18 '14 at 16:16
  • In windows you can do that with registry :) In linux with what? – Sepahrad Salour Aug 18 '14 at 17:22
4

This should work for a bash script. It will abort execution of the script if another instance of the external software was found. The key here is `pidof.

#!/bin/bash

# Abort startup if another instance was found    
pidof /path/to/software.executable > /dev/null && {
  echo Sorry. Only one instance allowed.
  exit
}

Alternatively you could use lock files, replace the running instance (i.e. first kill running instance, then allow run).

An expert user could however bypass this script and invoke the external command directly.

  • Thanks for your post BUT I don't want to restrict my service script's I wanna restrict USER to not run our SOFTWARE(Command) from linux (not our software) because it has been executed. – Sepahrad Salour Aug 18 '14 at 12:52
  • 2
    The code I provided will restrict USER (and everyone else) to not run the software while there is another instance running. If this is not what you want could you explain better? – LatinSuD Aug 18 '14 at 12:58
  • That's true but I have too add these codes to the software source code but I wanna init do that for me.... – Sepahrad Salour Aug 18 '14 at 13:07
  • Depending on how your users interact with your system, I would recommend only exposing them to a "launcher" script that you've written (and denying them access to the original launcher script). This script would look similar to the above answer – OldTinfoil Aug 18 '14 at 13:25
1

If we're constrained to only running something with init, then the only solution I can think of is to have a lightweight kill-script constantly monitoring what processes are running, and to terminate any additional processes.

But I would suggest that this is far from perfect, and could be bypassed relatively easily.

  • (Still think that a solution based on @LatinSud 's answer is more elegant) – OldTinfoil Aug 18 '14 at 13:25
1

Actually, the sensible way is to deny a user access by using the correct permissions and only allow root(esque) users run the software.

  • BTW, you could use crontab jobs to periodically restrict permissions on some file. – Basile Starynkevitch Aug 18 '14 at 17:28
  • Depending on how time-critical it is to ensure that no additional processes are running... – OldTinfoil Aug 18 '14 at 17:30
  • It's very critical job... – Sepahrad Salour Aug 18 '14 at 18:32
  • I suggest looking at my other answer, if that's the case – OldTinfoil Aug 18 '14 at 18:46
1

You can instruct init to run only one instance of your software — in fact, that will happen naturally if you start your software from init. But init's job has nothing to do with preventing other programs from starting other instances of your software.

If you want to ensure that only a single instance of your program runs, then instead of running your program directly, run a wrapper script that checks whether your software is already running. LatinSuD's answer gives an example of such a wrapper script.

Of course a user could bypass the wrapper script and run another instance of your software. But a user could also create an executable file that happens to be identical to yours and run that. So going via a wrapper script does implement the desired restriction in a way that makes sense.

Having multiple instances of the same executable running is not in itself a problem. What can be a problem is if they access the same resource: files, network ports, etc. If you need to protect against concurrent access to the same resource, then the possible methods depend on what the resource is. If the resource is a TCP or UDP port, only one process can listen on that port anyway. If the resource is a file, make sure that only the user running the instance started by an init script has the right to access that file.

1

Old thread but worth to put some notes here: In case of running the process under a parent like "/bin/bash /tmp/my_script.sh" or "python my_script.py", use the following:

pgrep -l my_script.sh >/dev/null && echo "Another process is running" || { 
do your stuff;
}

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.