Check if process is running (using process name) - go

Is there anyway in Go to check if a proces is running by searching by process name? I see ways to do it with PID, however I don't have the PID to search by. Thanks.

http://www.faqs.org/faqs/unix-faq/faq/part3/section-10.html,
Find PID of a Process by Name without Using popen() or system()
No direct way available. You can use os/exec with pidof or pgrep to do this. Or read into procfs.

Related

Fetching the right PID from Process.spawn

I am creating a CLI application using ruby. This app receives a path to an executable, runs it, fetches the PID and collects stack trace samples every N milliseconds in order to profile the executable.
I use Process.spawn like this:
pid = Process.spwan(ENV, executable)
The problem with pid is that it's not the executable's, but it is the PID of sh -c <EXECUTABLE>.
In order to fetch the right PID I use pidof after Process.spawn like this:
target_pid = `pidof -s #{executable}`
and then I use target_pid for profiling.
Is there a cleaner way to get target_pid using Ruby?
If you don't need the shell then use either of these two forms of calling:
pid = Process.spwan(ENV, executable, '--arg1')
and if you have no arguments then you would need to use this format instead:
pid = Process.spwan(ENV, [executable, 'name of executable'])
If you need the shell then you need to modify your executable variable to do something along these lines
cmd & echo "pid=$!"; fg
which means run your command in the background, obtain the pid of it which you can somehow communicate to your ruby process, then put that process in the foreground again.

How do I handle stopping my service?

I've turned a program I wrote into a service, and I have a bash script that runs to start up the actual program, since there are things that have to be started in a particular order. The contents of the startup script (called with start-stop-daemon from the init.d script look like :
./rfid_reader &
sleep 2
java ReaderClass &
This works fine, but how do I go about killing them when it comes time to stop the process?
I've seen pidfiles used, do I just get the PIDs of the two programs, write them to a file, and then kill them when it comes time to shut them down, or do I have to ps -ef | grep program to get their PIDs?
I don't think killall is a good idea to do this. You'd better to record the PID of the program started in background in some file(e.g. PID_FILE) and then kill $(<$PID_FILE) to stop it.
Please refer to this thread for how to get the PID the previous started background program.
Assuming you know the name of your program, you can kill them as below :
killall -KILL program_name

How to Turn Off a Program Running from .bash_profile

I am working with a Raspberry Pi running linux, and have modified the .bash_profile file in order to a run a program I've made automatically upon login. This works great, but I was wondering how to turn the program off, since for ctrl+c I would need to be running the program in the terminal.
You can use Kill to terminate program by its process Id, Top command will list all the processes, You can also use Pkill, which will Terminate the process by its name.
-9 option forces process to shut down, so its very commonly used.
Example:
kill -9 "Process ID without Quotation marks"
pkill -9 "Name with Quotation marks (Case Sensitive)"
Check this.
I always use
pkill - f processname

Find a process ID by name

How can I find a pid by name or full command line in Ruby, without calling an external executable?
I am sending SIGUSR2 to a process whose command line contained ruby job.rb. I would like to do the following without the call to pgrep:
uid = Process.uid
pid = `pgrep -f "ruby job.rb" -u #{uid}`.split("\n").first.to_i
Process.kill "USR2", pid
How to do this depends on your operating system. Assuming Linux, you can manually crawl the /proc filesystem and look for the right command line. However, this is the same thing that pgrep is doing, and will actually make the program less portable.
Something like this might work.
def get_pid(cmd)
Dir['/proc/[0-9]*/cmdline'].each do|p|
if File.read(p) == cmd
Process.kill( "USR2", p.split('/')[1] )
end
end
end
Just be careful poking around in /proc.
A quick google search came up with sys_proctable, which should let you do this in a portable way.
Disclaimer: I don't use Ruby, can't confirm if this works.
Debian based systems find pid with pidof command.
Some kill proccess function with ruby:
def killPid(cmd)
pid=exec("pidof #{cmd}")
Process.kill "USR2", pid
end

How can I find out what a command is executing in Terminal on MacOs

After I run a shell script (which call a bunch a other scripts depend on conditions. which is too complicated to understand), I can execute a command 'gdbclient' at my MacOS terminal.
But when I do 'which gdbclient' and 'alias gdbclient', it shows nothing.
Is there anyway for me to find out what 'gdbclient' is actually doing?
You can open up another terminal window and type: ps
That will list all the running processes.
If your script is running as a different user than the current one, you can use ps -ef to list all running processes.
If you know the PID of the process that ran your script, you can find all child processes via parent PID using ps -f | grep [pid]
You can use the Activity Monitor to check things out pretty thoroughly. To get the right privileges to see everything going on you can do:
sudo open /Applications/Utilities/Activity\ Monitor.app/
Dtrace can give you some helpful information: dtrace
to find process 'gdbclient':
ps aux | grep gdbclient
That wont tell you what it's "doing" but that it's running

Resources