How can I get su screen PID on Debian8?

Posted on

How can I get su screen PID on Debian8? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, debian, gnu-screen, su, pid.

I want to run this command by another user (so with su): screen -ALmdS server_mta ./mta-server -n

It’s okay: su - server_2 -c 'screen -ALmdS server_mta ./mta-server -n

But I need the PID of screen, but when i try to get, I got anoter PIDs.

I tried this method:

su - server_2 -c "screen -ALmdS server_mta ./mta-server -n >>~/outlog 2>&1 & echo $! "

But it was give 5996 PID, but when I check it with ps ax |grep server_mta the result was:

5997 ?        Ss     0:00 SCREEN -ALmdS server_mta ./mta-server -n
6023 pts/45   S+     0:00 grep server_mta

I tried this command:

su - server_2 -c 'screen -ALmdS server_mta ./mta-server -n > /dev/null & ps ax |grep  $! '

And the result was:

6829 ?        R      0:00 screen -ALmdS server_mta ./mta-server -n
6831 ?        S      0:00 grep 6829

I thought I got the PID, and when I checked again with ps ax |grep server_mta, I got an another PID. And the difference here is the screen is lowercase, and when I check in ps ax |grep server_mta, this is uppercase.

How can I got the PID of my screen process which is ran by another user with su?

Solution :

I found a solution:

su - server_2 -c "screen -ALmdS server_mta ./mta-server -n  >>/home/out 2>&1"; pgrep -u server_2 |  xargs ps -p | grep SCREEN | cut -f1 -d' ' | head -n 1

This return the PID of screen.

Leave a Reply

Your email address will not be published.