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?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.