How to find the real owner of a process after su - linux

We have a convention whereby developers get into a server with their own username, and then sudo su - django where django is the user our apps run under.
I need to find out which developer is running a script as django. With ps faux :
root 26438 0.0 0.0 90152 3320 ? Ss 10:38 0:00 \_ sshd: fred [priv]
fred 26444 0.0 0.0 90152 1852 ? S 10:38 0:00 | \_ sshd: fred#pts/0
fred 26445 0.0 0.0 66052 1560 pts/0 Ss 10:38 0:00 | \_ -bash
root 27923 0.0 0.0 101052 1336 pts/0 S 10:46 0:00 | \_ su - django
django 27924 0.0 0.0 66188 1752 pts/0 S 10:46 0:00 | \_ - bash
django 31760 0.0 0.5 227028 42320 pts/0 S+ 11:10 0:01 | \_ python target_script.py
I can easily see what fred is up to. However I need to write a script to act on this info, and I can find no way to pull out "fred" and "target_script.py" in one line with ps ... euser,ruser,suser,fuser all say "django." Will I need to fumble through this ps faux output to get the info I need?

I found this old post when trying to find the same basic information. The easiest way I found was to use the "loginuid" file under /proc/[pid]. For example:
cat /proc/${processid}/loginuid
Sorry for resurrecting such and old post, but maybe someone will find it useful.

You used su - django. The " - " will make the new shell a login shell (see manpage of su), which let the child process forget its parent uids. That's why euser,ruser,suser,fuser all say "django".
So yes, you may have to fumble through the parent process id, or through "ps faux".

Related

How do I find the name of a process in linux?

I'm really struggling with how to find processes by name in linux. I'm sure it's probably something simple that I'm missing.
Are you looking for the command ps ?
Here an example
nabil#LAPTOP:~$ ps xua | grep python
rootwsl 327 0.0 0.1 29568 17880 ? Ss Jan30 0:02 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
rootwsl 411 0.0 0.1 108116 20740 ? Ssl Jan30 0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
nabil 106387 0.0 0.0 3444 736 pts/1 S+ 23:26 0:00 grep --color=auto python

Run Webserver in the background

In order to have my site running on a webserver i have to manualy login into it with these commands:
ssh IP
add password
cd chn
source bin/activate
cd chn
python3 manage.py runserver IP:80
And that is it! How i can do all this process to run in the background?
PS. The server is running on Linux/Ubuntu14.0 i think. If i put & at the end of each line, i will see them in my terminal, but i am afraid that it will stop all, when i close my mac/termina etc.
root#localhost:~# ps aux | grep 1373
root 30873 0.0 0.0 11724 680 pts/1 S+ 14:35 0:00 grep --color=auto 1373
root#localhost:~# ps aux | grep 30827
root 30875 0.0 0.0 11724 676 pts/1 S+ 14:36 0:00 grep --color=auto 30827
root#localhost:~# ps aux | grep 30835
root 30835 0.2 3.0 100644 31024 pts/0 S 14:33 0:00 python3 manage.py runserver IP:80
root 30877 0.0 0.0 11724 676 pts/1 S+ 14:36 0:00 grep --color=auto 30835
Thank you!
it worked with this command Nohup python3 manage.py runserver 5.83.45.64:80 &

How does 'kill -STOP and kill -CONT' work?

I'm facing an issue.
We have a clean script using to clean old files, and sometimes we need stop it for and will start it again later. Like the below processes. We use kill -STOP $pid and kill -CONT $pid in check.sh to control the clean.sh, $pid is all the pids of clean.sh (at there, they are 23939, 25804):
root 4321 0.0 0.0 74876 1184 ? Ss 2015 0:25 crond
root 23547 0.0 0.0 102084 1604 ? S 2015 0:00 \_ crond
root 23571 0.0 0.0 8728 972 ? Ss 2015 0:00 \_ /bin/bash -c bash /home/test/sbin/check.sh >>/home/test/log/check.log 2>&1
root 23577 0.0 0.0 8732 1092 ? S 2015 0:00 \_ bash /home/test/sbin/check.sh
root 23939 0.0 0.0 8860 1192 ? S 2015 0:45 \_ bash /home/test/bin/clean.sh 30
root 25804 0.0 0.0 8860 620 ? S 2015 0:00 \_ bash /home/test/bin/clean.sh 30
root 25805 0.0 0.0 14432 284 ? T 2015 0:00 \_ ls -d ./455bb4cba6142427156d2b959b8b0986/120x60/ ./455bb4cba6142427156d2b959b8b0986/80x
root 25808 0.0 0.0 3816 432 ? S 2015 0:00 \_ wc -l
Once the check.sh stopped clean.sh, hours later, check.sh started clean.sh, but there is a strange thing, after a stop and continue, there is a child process 'ls -d ....', it's still stopping.
Could you tell me if it's caused by wrong use of the signal? And how can I modify it?
ok, same like my description is not clear, my bad English...
Not sure what's the reason, but there is a way to sovle it:
kill -CONT $pid
pkill -CONT -P $pid
This will continue the child process.

Can I delete [php] <defunct> processes

I made a PHP script which reads some data and starts a new process with pctnl_fork to do some work with the data. When the child processes are finished they stay in as process. ps aux shows me a list of processes like
demo 32229 0.0 0.0 0 0 pts/0 Z 12:23 0:00 [php] <defunct>
demo 32251 0.0 0.0 0 0 pts/0 Z 11:50 0:00 [php] <defunct>
demo 32284 0.0 0.0 0 0 pts/0 Z 11:50 0:00 [php] <defunct>
demo 32298 0.0 0.0 0 0 pts/0 Z 12:56 0:00 [php] <defunct>
demo 32303 0.0 0.0 0 0 pts/0 Z 11:50 0:00 [php] <defunct>
demo 32316 0.0 0.0 0 0 pts/0 Z 12:23 0:00 [php] <defunct>
Can I safely kill this processes as the parent process is still creating new processes to do some work? I know I should probably start the command piping to stderr (according to this topic Insane crond behavior. keeps making defunct bash processes now it is too late.
The parent process is running in the background.
After doing some research, it appears you'll have to kill the parent process for these defunct child processes to go away.
The most pertinent quote from the link says "You cannot kill a defunct process (a.k.a zombie) as it is already dead."
So as U said,
a command in your PHP, like
$a;
pcntl_waitpid($child_pid, $a, WNOHANG|WUNTRACED);
will do the job.
to me works, just fine !

How to see a terminal output from a previously closed terminal

I connect to a remote server using SSH
I was compiling using cmake and then make, it's not common to have a progress percentage in compilation process, but this time it has. I was watching the compilation process until my internet connection failed, so puTTY closed the session and I had to connect again to my server. I though that all the progress was lost, but i first make sure by watching the processes list by ps aux command, and I noticed that the processes related to the compilation are still running:
1160 tty1 Ss+ 0:00 /sbin/mingetty tty1
2265 ? Ss 0:00 sshd: root#pts/1
2269 pts/1 Ss 0:00 -bash
2353 pts/1 S+ 0:00 make
2356 pts/1 S+ 0:00 make -f CMakeFiles/Makefile2 all
2952 ? S 0:00 pickup -l -t fifo -u
3085 ? Ss 0:00 sshd: root#pts/0
3089 pts/0 Ss 0:00 -bash
3500 pts/1 S+ 0:01 make -f src/compiler/CMakeFiles/hphp_analysis.dir/bui
3509 pts/1 S+ 0:00 /bin/sh -c cd /root/hiphop/hiphop-php/src/compiler &&
3510 pts/1 S+ 0:00 /usr/bin/g++44 -DNO_JEMALLOC=1 -DNO_TCMALLOC=1 -D_GNU
3511 pts/1 R+ 0:03 /usr/libexec/gcc/x86_64-redhat-linux6E/4.4.4/cc1plus
3512 pts/0 R+ 0:00 ps ax
I would like to know if is possible to watch the current progress of the compilation by watching the previously closed terminal output. Something similar like 'cat /dev/vcsa1' or something
As per the comment above, you should have used screen.
As it is, you could try to peek at the file descriptors used by sshd and the shell that you started, but I don't think this will get you very far.

Resources