0

I've got a Jenkins hooked up to a Bitbucket repository. When something is pushed to the repository the Jenkins starts a job. This job gets all data from the repository (server.py: which is basically a bjoern server). What I want to do now is stopping the server.py-process which is running and restart it with the changed data. This should be done by the following bash script which is run by the jenkins:

#!/bin/bash
PID=$(ps ax | grep 'server.py')
kill $PID

My problem is that the jenkins user on my ubuntu server can't kill the server.py process. How can kill the process?

Or is it possible to catch the server.py process and stop the bjoern server instance with a function call?

Fauphi
  • 360
  • 1
  • 2
  • 11
  • Is the python process running on the same machine where you're running the job? Also, what's the error you're getting? – Technext Sep 27 '14 at 12:41
  • Solved the problem in editing the /etc/sudoers. I added the following line "jenkins ALL= NOPASSWD: ALL". I know that this is not very secure. How can I restrict the right to call kill/sh/... on a specific folder? – Fauphi Sep 27 '14 at 15:35
  • I hope you must have gone through `sudoers` file and how you can benefit with the options provided in it. I'm not sure what you mean by restricting usage of `kill to a specific folder`. You can however, restrict other command usage to a specific folder such as a specific user can only run `chmod` or stuff like that in a given folder (or sub-folders). The thing is, if you have a script which is managed solely by you, these things should not matter much `as long as the script isn't doing something really bad`. I mean `rm` kind of stuff. – Technext Sep 27 '14 at 16:54
  • One more thing that i would like to mention is that when you're trying out commands to be run by some application, then in such cases, you should always try running the command using command line once and see how it goes. After you're satisfied, only then you should hand over such responsibilities to others (Jenkins, for instance). It also saves time that you might spent on debugging because there will be more variables involved. :) – Technext Sep 27 '14 at 17:01

1 Answers1

2

Irrespective of where you're running the process, the way you're trying to retrieve the PID is incorrect.

PID=$(ps ax | grep 'server.py') will not get you the PID. It will return more than just the PID.

Check the example below:

[root@jenkins ]# ps -ef | grep xinetd
root      1528     1  0 Aug28 ?        00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
root      9827  9806  0 17:34 pts/1    00:00:00 grep xinetd

As you can realize from the above output, you will have to filter out the process which you need, which can be done by:

[root@jenkins ]# ps -ef | grep xinetd | grep -v grep
root      1528     1  0 Aug28 ?        00:00:00 xinetd -stayalive -pidfile /var/run/xinetd.pid

As you can see from the above output, it returns us just the line from which we need to fetch the PID. This last command gives you what you want i.e., the PID

[root@jenkins ]# ps -ef | grep xinetd | grep -v grep | awk -F' ' '{print $2}'
1528

So, instead of what you are using, use:

PID=$(ps -ef | grep xinetd | grep -v grep | awk -F' ' '{print $2}')

OR

If you already know the process name, then you could simply use the command pidof.

[root@jenkins ]# pidof xinetd
1528
Technext
  • 7,887
  • 9
  • 48
  • 76