shell script for killing a linux process

hi
i am starting a java application on a linux box with the following command -
daemon --user=flocksnippet "java -jar snipper.jar" &
this starts a java process in background

i need to write a shell script to kill this particular process so that when i run the script it checks if the process is running if so kill it

please help me write shell script for this.

thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

savoneCommented:
Try this:

kill  `ps -ef | grep -v grep | grep "snipper" | awk '{print $2}'`

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmcgOwnerCommented:
Check to see if your daemon command supports --name and --stop options. These options give a more transparent way to control background daemons and provide for the possibility of having more than one daemon, perhaps for slightly different purposes but using the same program, to coexist (by using different --name identifiers). Also, unless you are running as root, the effective userid when the daemon starts needs to be in effect when killing the daemon, i.e. the --user options need to match.
Rohit BajajAuthor Commented:
hi
the grep command above gives me 3 pids
2382
2383
2384
i need to findout which one is the main pid killing which rest automatically gets killed.. else i think i can kill all
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Rohit BajajAuthor Commented:
i am sourcing daemon from /etc/init.d/functions
looks like it doesnot have --name option
savoneCommented:
can you show me the output of:

ps -ef | grep -v grep | grep "snipper"
jmcgOwnerCommented:
If it takes a --name, it might also take --pidfile.

As for your list of three PIDs, I'd expect that killing the lowest-numbered one will typically work, but if you catch the system just right, you might see the PIDs wrap around so "lowest-numbered" is not the same as "first one started, parent of the others".

But if you're writing this as an init script to live in init.d, you should perhaps look at several of the other init scripts to match their behavior when given "start", "restart", or "stop" options. You should prefer to control the process through a recorded PID rather than any search of the output of a ps command.
Rohit BajajAuthor Commented:
it does have a --pidfile option though but not the --name option
jmcgOwnerCommented:
Sorry, I meant "if it takes a --user option".
Rohit BajajAuthor Commented:
how do i use --pidfile option to run my daemon
can this be used also to stop the process ?
jmcgOwnerCommented:
You give the --pidfile option a filename and it places the processID of the background program in that file (there's a conventional place for pidfiles or you may be able to tell it to put the pidfile somewhere else).

When the time comes to kill the process, you read in the PID from that file.

It could be as simple as something like

kill `cat /var/run/pid/mydaemon`

Open in new window

Gerwin Jansen, EE MVETopic Advisor Commented:
Try starting the daemon by giving it a name, add this to your commandline:

--name=<name>

Then when you want it stopped, try referring that <name>:

daemon --user=flocksnippet --name=<name> --stop
Rohit BajajAuthor Commented:
Hi,
using --pidfile option is not working i specified --pidfile=snippet.pid  but the file is not getting created anywhere...
Rohit BajajAuthor Commented:
In the output of  ps -ef | grep -v grep | grep "snippet"
There are 4 processes 2 started by root and 2 started by user flocksnippet. This is because i am starting the process
like service snippet start under roor
I need to kill the ones started by flocksnippet...
but greping is not working as it shows username like flocksn+
Gerwin Jansen, EE MVETopic Advisor Commented:
>> In the output of  ps -ef | grep -v grep | grep "snippet"
Please post the output of ps -ef commands as requested.

>> but greping is not working as it shows username like flocksn+
Then could you grep for flocksn instead?

Did you try my --name suggestion?
Rohit BajajAuthor Commented:
HI,
The output is in following format :
UID  PID PPID C STOME TTY TIME CMD
I am unable to paste complete output currently as i am running on virtual box and for some reason unable to copy it..
will try to see if i can paste...
--name is not listed in daemon function.. The file i have in my init.d/functions you can see at :
http://bash.cyberciti.biz/guide//etc/init.d/functions

I will try flocksn approach
Gerwin Jansen, EE MVETopic Advisor Commented:
>> I am unable to paste complete output currently as i am running on virtual box and for some reason unable to copy it..
will try to see if i can paste...

You can redirect the output of the ps command to a file by adding >> somefile.txt then transfer that file.

>> virtual box
If this is VMware, install VMware tools if you can.
Rohit BajajAuthor Commented:
Output of ps -ef

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 04:28 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
root         2     0  0 04:28 ?        00:00:00 [kthreadd]
root         3     2  0 04:28 ?        00:00:00 [ksoftirqd/0]
root         5     2  0 04:28 ?        00:00:00 [kworker/0:0H]
root         6     2  0 04:28 ?        00:00:00 [kworker/u2:0]
root         7     2  0 04:28 ?        00:00:00 [migration/0]
root         8     2  0 04:28 ?        00:00:00 [rcu_bh]
root         9     2  0 04:28 ?        00:00:00 [rcuob/0]
root        10     2  0 04:28 ?        00:00:00 [rcu_sched]
root        11     2  0 04:28 ?        00:00:00 [rcuos/0]
root        12     2  0 04:28 ?        00:00:00 [watchdog/0]
root        13     2  0 04:28 ?        00:00:00 [khelper]
root        14     2  0 04:28 ?        00:00:00 [kdevtmpfs]
root        15     2  0 04:28 ?        00:00:00 [netns]
root        16     2  0 04:28 ?        00:00:00 [writeback]
root        17     2  0 04:28 ?        00:00:00 [kintegrityd]
root        18     2  0 04:28 ?        00:00:00 [bioset]
root        19     2  0 04:28 ?        00:00:00 [kblockd]
root        20     2  0 04:28 ?        00:00:00 [khubd]
root        21     2  0 04:28 ?        00:00:00 [md]
root        24     2  0 04:28 ?        00:00:00 [khungtaskd]
root        25     2  0 04:28 ?        00:00:00 [kswapd0]
root        26     2  0 04:28 ?        00:00:00 [ksmd]
root        27     2  0 04:28 ?        00:00:00 [khugepaged]
root        28     2  0 04:28 ?        00:00:00 [fsnotify_mark]
root        29     2  0 04:28 ?        00:00:00 [crypto]
root        37     2  0 04:28 ?        00:00:00 [kthrotld]
root        38     2  0 04:28 ?        00:00:00 [kworker/u2:1]
root        39     2  0 04:28 ?        00:00:00 [kmpath_rdacd]
root        40     2  0 04:28 ?        00:00:00 [kpsmoused]
root        59     2  0 04:28 ?        00:00:00 [deferwq]
root        82     2  0 04:28 ?        00:00:00 [kauditd]
root       247     2  0 04:28 ?        00:00:00 [ata_sff]
root       265     2  0 04:28 ?        00:00:00 [scsi_eh_0]
root       266     2  0 04:28 ?        00:00:00 [scsi_tmf_0]
root       267     2  0 04:28 ?        00:00:00 [scsi_eh_1]
root       268     2  0 04:28 ?        00:00:00 [scsi_tmf_1]
root       280     2  0 04:28 ?        00:00:00 [kworker/0:1H]
root       342     2  0 04:28 ?        00:00:00 [kdmflush]
root       343     2  0 04:28 ?        00:00:00 [bioset]
root       352     2  0 04:28 ?        00:00:00 [kdmflush]
root       353     2  0 04:28 ?        00:00:00 [bioset]
root       369     2  0 04:28 ?        00:00:00 [xfsalloc]
root       370     2  0 04:28 ?        00:00:00 [xfs_mru_cache]
root       371     2  0 04:28 ?        00:00:00 [xfs-buf/dm-0]
root       372     2  0 04:28 ?        00:00:00 [xfs-data/dm-0]
root       373     2  0 04:28 ?        00:00:00 [xfs-conv/dm-0]
root       374     2  0 04:28 ?        00:00:00 [xfs-cil/dm-0]
root       375     2  0 04:28 ?        00:00:01 [xfsaild/dm-0]
root       444     1  0 04:28 ?        00:00:00 /usr/lib/systemd/systemd-journald
root       455     1  0 04:28 ?        00:00:00 /usr/sbin/lvmetad -f
root       462     1  0 04:28 ?        00:00:00 /usr/lib/systemd/systemd-udevd
root       477     2  0 04:28 ?        00:00:00 [iprt-VBoxWQueue]
root       523     2  0 04:28 ?        00:00:00 [xfs-buf/sda1]
root       524     2  0 04:28 ?        00:00:00 [xfs-data/sda1]
root       525     2  0 04:28 ?        00:00:00 [xfs-conv/sda1]
root       526     2  0 04:28 ?        00:00:00 [xfs-cil/sda1]
root       529     2  0 04:28 ?        00:00:00 [xfsaild/sda1]
root       560     1  0 04:28 ?        00:00:00 /sbin/auditd -n
root       586     1  0 04:28 ?        00:00:00 /usr/sbin/rsyslogd -n
root       587     1  0 04:28 ?        00:00:00 /usr/bin/python -Es /usr/sbin/tuned -l -P
root       595     1  0 04:28 ?        00:00:00 /usr/lib/systemd/systemd-logind
dbus       596     1  0 04:28 ?        00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root       597     1  0 04:28 ?        00:00:00 /usr/sbin/crond -n
root       604     1  0 04:28 ?        00:00:00 login -- root
root       687     1  0 04:28 ?        00:00:01 /usr/sbin/VBoxService --pidfile /var/lock/subsys/vboxadd-service
root       723     1  0 04:28 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
polkitd    786     1  0 04:28 ?        00:00:00 /usr/lib/polkit-1/polkitd --no-debug
root      1077     1  0 04:28 ?        00:00:00 /usr/sbin/sshd -D
root      1425     1  0 04:28 ?        00:00:00 /usr/libexec/postfix/master -w
postfix   1437  1425  0 04:28 ?        00:00:00 qmgr -l -t unix -u
root      1498   723  0 04:28 ?        00:00:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-enp0s8.pid -lf /var/lib/NetworkManager/dhclient-dbc16db1-b099-40be-8551-a35d7d522a50-enp0s8.lease -cf /var/lib/NetworkManager/dhclient-enp0s8.conf enp0s8
root      2361   604  0 04:28 tty1     00:00:01 -bash
root      2634     1  0 05:06 tty6     00:00:00 /sbin/agetty --noclear tty6
root      2635     1  0 05:06 tty5     00:00:00 /sbin/agetty --noclear tty5
root      2636     1  0 05:06 tty4     00:00:00 /sbin/agetty --noclear tty4
root      2637     1  0 05:06 tty3     00:00:00 /sbin/agetty --noclear tty3
root      2638     1  0 05:06 tty2     00:00:00 /sbin/agetty --noclear tty2
root      2857     1  0 05:13 ?        00:00:00 dhclient
root      2883   723  0 05:13 ?        00:00:00 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-enp0s3.pid -lf /var/lib/NetworkManager/dhclient-906f24d1-3164-4dbd-b527-d2bc5b0ede07-enp0s3.lease -cf /var/lib/NetworkManager/dhclient-enp0s3.conf enp0s3
root      3409     2  0 05:26 ?        00:00:00 [kworker/0:0]
root      3432     2  0 05:27 ?        00:00:00 [kworker/0:2]
postfix   4166  1425  0 06:08 ?        00:00:00 pickup -l -t unix -u
root      4244     2  0 06:13 ?        00:00:00 [kworker/0:3]
root      4245     2  0 06:18 ?        00:00:00 [kworker/0:1]
root      4246     2  0 06:18 ?        00:00:00 [kworker/0:4]
root      4269     1  0 06:19 ?        00:00:00 /bin/bash /etc/rc.d/init.d/snippet start
root      4271  4269  0 06:19 ?        00:00:00 runuser -s /bin/bash flocksnippet -c ulimit -S -c 0 >/dev/null 2>&1 ; java -DFLOCK_APPS_CONFIG=staging -jar snippet.jar
flocksn+  4272  4271  0 06:19 ?        00:00:00 bash -c ulimit -S -c 0 >/dev/null 2>&1 ; java -DFLOCK_APPS_CONFIG=staging -jar snippet.jar
flocksn+  4273  4272 18 06:19 ?        00:00:02 java -DFLOCK_APPS_CONFIG=staging -jar snippet.jar
root      4297  2361  0 06:19 tty1     00:00:00 ps -ef

Open in new window

jmcgOwnerCommented:
Looking at your functions file, it doesn't seem to invoke the command asynchronously nor do anything to collect the PID of the resulting process. That file is labeled a sample. Can you say exactly which version of Linux you are using?
Gerwin Jansen, EE MVETopic Advisor Commented:
grep for ^flocksn - then for -v bash and kill the process

Test:

echo $(ps -ef | grep ^flocksn | grep -v bash | awk '{print $2}')

Kill:

kill $(ps -ef | grep ^flocksn | grep -v bash | awk '{print $2}')

If it doesn't shutdown, add -9 to kill (eg: kill -9 ...)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.