0

I'm fairly new to Linux

I need to create a Java process that is an orphan (not a child of any process) and it needs to either be a service or have a unique process name so that I can stop it whenever I like.

Basically I'm trying to get Gitlab to deploy a Java server from CI. Every CI build needs to identify and kill a server instance if one is already running, and make a new one. (The server must also run as an orphan or else the CI build gets stuck waiting for the server to terminate.)

Sure, I can just kill all running Java processes but I'd like a cleaner way in case I ever have a Java process running that is not the server.

I tried to make an alias but bash doesn't see it unless I try the alias in terminal. (How to create a service on Ubuntu Upstart I also tried using a function by inserting into .bashrc and .bash_aliases by following instructions from How to run an alias in a shell script? to check if it would work like an alias and have a custom process name but it also failed.

I would make a service but I'm new to Linux and don't know how, plus the directory where the jar is stored changes every build. I don't know how I would pass the directory to the service so that Java knows what it's running.

2

The traditional Unix solution for this is to store the PID in a file with a known name in a known location, then use the contents of that file when needed. This is often, if unimaginatively, called a pid-file.

For example, when the sshd daemon/service is started, its PID is written to a file named sshd.pid. Later when it is desired to stop sshd, or signal it to reconfigure, this file is read, and if sshd is stopped the file is removed. The 'normal' location for a pid-file is in or under /var/run but you sometimes find older systems or programs using /etc or even /, and nothing prevents you from using something like /home/testcontroller if you want.

ADDED: Either the creator that starts the process must write its pid-file or the process itself must do so; some programs do the latter automatically but java doesn't (and can't easily be changed to) so you need the former. I don't know and you don't say how gitlab runs things but you mention bash; if it is using a bash script you control that does something more or less like

$somejre/java -Xmx99G -Dsome=config -Dmore=config -jar $appjar some args & # & for nowait aka background

then add immediately after that

echo $! >$somelocation/myjava.pid # $! is the PID the most recent 'nowait' process 

and when you want to stop it, at latest before starting a new one using the same file, do (as the same userid) something like

if [ -e $somewhere/myjava.pid ] && ps -p $(cat $somewhere/myjava.pid) >/dev/null
then kill $(cat $somewhere/myjava.pid); rm -f $somewhere/myjava.pid; fi
# the default signal (TERM) is usually enough for Java, but if not
# (perhaps after a brief wait) use something stronger like -KILL

If someone else is running Java, this method ignores them. If you want several Java processes OF YOUR OWN, assign separate filenames. If you have so many (maybe hundreds) that separate filenames don't work, you have a harder problem.

  • So if I start java I'll look for java.pid ? but over time there could be multiple java.pids so how will I know which one is the one I want to kill? Is there a way to record a processes' PID manually as soon as its run, even if there are already multiple processes with the same name? – CausingUnderflowsEverywhere Aug 17 '16 at 2:38
  • @CausingUnderflowsEverywhere details added – dave_thompson_085 Aug 18 '16 at 16:27
  • Oh wow neat, so $! stores the PID of the last background job. This seems to be exactly what I need. I might also need nohup or disown because the parent bash/terminal is meant to die after launching the java server. – CausingUnderflowsEverywhere Aug 19 '16 at 17:35

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.