4

I've googled this question, but never found anything useful for my particular case.

So, what I'm trying to figure out is the PID of a certain command that is running so I can kill it if necessary. I know it's possible to get the PID of a command by typing echo $! So supposedly

my_command & echo $!

should give me the PID. But this isn't the case, and I think I know why: My command is as follows:

screen -d -m -S Radio /path/to/folder -f frequency -r path/to/song

which opens a detached screen first and then types the command so that it gets executed and keeps on running in the background. This way the PID that echo shows me is the wrong one. I'm guessing it shows me PID of screen -d -m -S Radio /path/to/folder -f frequency -r path/to/song instead of the PID of the command run in the new terminal created by screen.

But there's another problem to it: when I run screen -ls in the terminal, the command that is running in the background doesn't show up! I'm fairly certain it's running because the Pi is constantly at 25% CPU usage (instead of the 0% or 1% usually) and when I type ps au I can actually see the command and the PID.

So now I'm calling the community: any idea on how I could find the PID of that certain command in the new terminal? I'm writing a bash script, so it has to be possible to obtain the PID through code. Perfect would be a command that stores the PID in a variable!

phuclv
  • 37,963
  • 15
  • 156
  • 475
Zebiano
  • 373
  • 5
  • 13
  • SO is for programming questions, not questions about using or configuring Linux. SuperUser.com or unix.stackexchange.com would be better places for questions like this. – Barmar Aug 03 '17 at 21:46
  • 1
    `pgrep -f search_pattern` -- add the `-l` flag if the output is for human consumption. – glenn jackman Aug 03 '17 at 21:48
  • Thanks @glennjackman , your comment worked for me. Somehow I now have the PID of the command! – Zebiano Aug 03 '17 at 22:16

2 Answers2

5

Thanks to @glennjackman I managed to get the PID I wanted with a simple pgrep search_word. At first it wasn't working, but somehow I made it work after some trial and error. For those wanting the PID on a variable, just type

pid=$(pgrep search_word)

Regarding the problem with screen -ls not showing my detached session, it's still not solved, but I'm not bothered with it. Thanks again for solving my problem @glennjackman !

EDIT:

Second problem solved, check the comments on berends answer.

Zebiano
  • 373
  • 5
  • 13
2

You can easily find out all the running process and their PID by writing (for example):

ps aux

The process is run by screen so you can probably find it easier by writing:

ps aux | grep screen

For more info about ps and the parameters I used check (quick google) -> https://www.lifewire.com/g00/uses-of-linux-ps-command-4058715?i10c

EDIT: You can use this command with bash scripting as well.

berend
  • 553
  • 2
  • 12
  • Thank you. The grep somehow helped me, but as I said: I need it to be in a script. I know how to find it on my own on the the terminal, but I need my script to know its PID and writing `ps aux | grep screen` in the script doesn't help much... Also, there will always be at least two results out of that command in my case. – Zebiano Aug 03 '17 at 21:25
  • Okay lets take a step back, if it is running in screen - you should be able to see it with screen -ls. Are you running the screen -ls command as the same user as the user who owns the screen session? – berend Aug 03 '17 at 21:28
  • To be honest, I have no idea... I know there's only one user on my raspberrypi (user Pi) and that the terminal always has `pi@raspberrypi`. So I'm assuming yes, I am running it as the same user. – Zebiano Aug 03 '17 at 21:34
  • Did you start the screen manually? I would really try to figure that out first, because I cant think of any other reason why you would not be able to see it with screen -ls other then it being run by as another user or not running at all. Check what is inside the /var/run/screen, you should be able to find all screens under each user there. – berend Aug 03 '17 at 21:38
  • No, I didnt start it manually. The command I wrote above starts the screen through my script. Basically opens screen in detached mode and runs there. But i just tested running a `screen -d -m -S Test sleep 999` and the command in my actual script, both manually in the terminal and surprisingly Test shows up but my other command from the script doesnt... – Zebiano Aug 03 '17 at 21:48
  • 1
    Using `ps` in scripts in not considered good form when alternatives are available -- it's extremely error-prone; `grep foo` might find someone running `vim foo.conf`. [BashFAQ #33](http://mywiki.wooledge.org/BashFAQ/033) discusses alternatives (and, particular, how those processes should be used if the context is any kind of daemon management; [ProcessManagement](http://mywiki.wooledge.org/ProcessManagement) is also pertinent). – Charles Duffy Aug 03 '17 at 22:29
  • @berend I figured out why `screen -ls` wasnt showing up my detached session. I ran screen once at the start of the program and inside that new screen session I started another `screen` session. Since it was a "screen inside another screen" it wouldnt show up one of those screens (the most inner one). – Zebiano Aug 06 '17 at 20:02