I use Linux Mint as my default OS.
I use jboss and apache for my development.
But I don’t enjoy using default start up and shut down scripts since their process gets attached to the terminal I used to start them.
I faced the same problem when running jboss in my virtualbox-vagrant powered virtual environment.
So I wrote this script to start and stop my server.
This script starts the process, and the process-id (pid) is not saved to a file, thus making it generic enough to be used to start and stop any process.
I have used a lot of variables to divide the commands into parts that are easy to understand.
- Save The below script to a file with ‘.sh’ extension
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #!/bin/sh processName="services"; echoName="jboss services process"; processDetails="ps -ef | grep $processName"; totalRunningProcess=$(eval $processDetails | wc -l); usage="Usage : $0 start | stop"; processStartCommand="nohup /home/vaio/data/devenv/jboss-4.2.2.GA_3/bin/run.sh -b 0.0.0.0 -c $processName > /dev/null 2>&1 &"; commandToGetProcessPids="eval $processDetails | tr -s ' ' | cut -d ' ' -f 2"; if [ $# -lt 1 ] then echo $usage; fi case "$1" in "stop" ) if [ $totalRunningProcess -eq "1" ] then echo "$echoName is not running...."; else echo "stopping $echoName...."; processToKill=$($commandToGetProcessPids); processToKillPidsArray=$(echo $processToKill); set -- $processToKillPidsArray; kill -9 $1 $2; echo "$echoName stopped...."; fi ;; "start" ) if [ $totalRunningProcess -gt "1" ] then echo "$echoName is already running...."; else echo "starting $echoName...."; eval $processStartCommand; fi ;; *) echo $usage; ;; esac
- Make the script executable by using the below command
1 chmod u+x script.sh
processName - this is the name of the process you want to kill.
echoName - this is the output that you want to show to the user.
processDetails - this command will get all the processes with the name that you want to kill.
1 $ ps -ef
This command will give you the list of all the running processes.
Now let’s assume you are running a java process.
1 $ ps -ef | grep java
This command will show you all the process name with java in it.
totalRunningProcess - this is to count total number pf processes.
You can do the same with
1 $ ps -ef | grep -c java
I wanted to reuse the variable so, I used word count (wc) with -l (count lines).
usage - this is to display to user how to use the program
processStartCommand - This contains the location of process that you want to start, here I have added these in the end.
- /dev/null 2>&1 &
/dev/null this means to redirect the default output to null which never cares what it receives.
2>&1 this means to also redirect the error output to the same /dev/null
2 is for error output
> means to redirect
&1 is for first output that was /dev/null
& is to run this in background
commandToGetProcessPids - This command gets all the process running with the process name that I defined and then translates or delete characters with repeating sequence (here ‘ ‘).
Then the remaining text has been split using cut command parameters:
-f 2 is used to get the process ids
eval is used to execute the command.
to remove spaces from the text then provide the required section of the string.