I have a program that is running as root. This app calls another program (processA) to run. When processA is running, it is owned by root but I want owner of it to be the current user logged on. How to do it?
Well it's a little bit tricky... Depends if it's a daemon (service) or you run this command/app.
For the 2nd case you can use "su" command.
Here's a short example.
1. I create o simple script with following content (it will sleep in background for 100 seconds and will output the process list coresponding to this script):
#!/bin/bash
sleep 100 &
ps faux | grep test.sh
2. I run the "su" command like this (I'm currently logged in as "root" and I want to run this script as "sandbox" user):
su - sandbox -c ./test.sh
sandbox = the username that will run this command.
-c ./test.sh = means it will execute this command
3. Output (first column = the user that owns this process):
root#i6:/web-storage/sandbox# su - sandbox -c ./test.sh
sandbox 18149 0.0 0.0 31284 1196 pts/0 S+ 20:13 0:00 \_ su - sandbox -c ./test.sh
sandbox 18150 0.0 0.0 8944 1160 pts/0 S+ 20:13 0:00 \_ /bin/bash ./test.sh
sandbox 18155 0.0 0.0 3956 644 pts/0 S+ 20:13 0:00 \_ grep test.sh
root#i6:/web-storage/sandbox#
I hope it will help,
Stefan
Related
When I ssh to a remote host, Is it possible that I get the remote bash pid in my local machine? or vice versa?
in local terminal, I simply do:
ssh user#remote.com
for example, I know the ssh pid in local machine:
ps faux
...
shunh 32683 0.1 0.0 121148 5116 pts/2 Ss 07:02 0:00 \_ -bash
shunh 33079 0.6 0.0 189360 8484 pts/2 S+ 07:03 0:00 \_ ssh -X remote_host
and in remote_host:
ps faux
...
root 214679 0.0 0.0 158820 5724 ? Ss 07:03 0:00 \_ sshd: shunh [priv]
shunh 214681 0.0 0.0 158820 2556 ? S 07:03 0:00 \_ sshd: shunh#pts/1
shunh 214682 0.0 0.0 121124 3608 pts/1 Ss 07:03 0:00 \_ -bash
I wanna know if I can read the pid number "214682" in my local machine?
ssh user#remote.com 'echo $$'
Refer to the Bash manual, 3.4.2 Special Parameters:
$
($$) Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.
I exec into Docker container with docker exec -it container-name bash
Inside container I run command ps aux | grep processName
I receive a PID and after that I run:
kill processId but receive:
-bash: kill: (21456) - No such process
Am I missing something or? I know that Docker shows different process IDs from top command inside the host and ps aux inside the container (How to kill process inside container? Docker top command), but I am running this from inside container?
That response is because the process you are trying to kill is not existing at the moment of killing it. For example, if you launch ps aux you can get an output like this inside a container (it depends of the container of course):
oot#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 15 0.0 0.0 36840 2904 pts/0 R+ 13:57 0:00 ps aux
Then if you try to kill process with PID 15 you'll get the error because PID 15 is finished at the moment of trying to kill it. The ps command terminates after showing you the processes info. So:
root#69fbbc0ff80d:/# kill 15
bash: kill: (15) - No such process
In a docker container you can kill process in the same way as normal excepting the root process (id 1). You can't kill it:
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 16 0.0 0.0 36840 2952 pts/0 R+ 13:59 0:00 ps aux
root#69fbbc0ff80d:/# kill 1
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 17 0.0 0.0 36840 2916 pts/0 R+ 13:59 0:00 ps aux
As you can see you can't kill it. Anyway if you want to proof that you can kill processes you can do:
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 18 0.0 0.0 36840 3064 pts/0 R+ 14:01 0:00 ps aux
root#69fbbc0ff80d:/# sleep 1000 &
[1] 19
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 19 0.0 0.0 4372 724 pts/0 S 14:01 0:00 sleep 1000
root 20 0.0 0.0 36840 3016 pts/0 R+ 14:01 0:00 ps aux
root#69fbbc0ff80d:/# kill 19
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 21 0.0 0.0 36840 2824 pts/0 R+ 14:01 0:00 ps aux
[1]+ Terminated sleep 1000
Hope it helps.
I'm writing a code which :
put all xsessions name in a file.
while not EOF , assign a line to a
variable, then pass it to pgrep, line by line.
Output pgrep in terminal.
There is several way to print the desktop environnement,session/window manager.
But i couldn't find one wich work for all xsession(/usr/share/xsessions).
After many try , it still doesn't work.
There is a problem with the loop.
ls /usr/share/xsessions/>all_xsessions;while [ "$var" != "" ]; do let x=x+1; var=$(sed "${x}q;d" all_xsessions); pgrep -lu $(whoami) $var; done
Unfortunately, there is no real standart way or universal environmental variable for that.
Most of information can be get from printenv but variable names differ a lot.
For example, in arch-linux with xfce, where sessions are run directly from windows-manager, you can get PID from $SESSION_MANAGER variable.
But in ubuntu, that is the most popular desktop distro now, your x-session is in fact upstart-session, that starts everything needed. And the variable is $UPSTART_SESSION.
And in xubuntu you have BOTH $SESSION_MANAGER and $UPSTART_SESSION variables.
There is also $DESKTOP_SESSION, it exists almost everywhere and contains some pretty name for desktop-environment, like xfce, ubuntu of xubuntu.
So, that can differ from distro to distro, but in most cases that script will work for you.
#!/bin/bash
[[ -n $SESSION_MANAGER ]] && pid="${SESSION_MANAGER##*/}"
[[ -n $UPSTART_SESSION ]] && pid="${UPSTART_SESSION##*/}"
process_name=$(cat /proc/$pid/cmdline)
pretty_name=$DESKTOP_SESSION
echo "$process_name : $pid : $pretty_name"
And output will be like that:
# Arch with xfce
xfce4-session : 3021 : xfce
# Ubuntu
/sbin/upstart--user : 3153 : ubuntu
# Xubuntu
init--user : 2343 : xubuntu
EDIT: Your method can work in some cases, but most of the time it will fail. Just because right now on ubuntu I have:
$ ls -1 /usr/share/xsessions/
openbox.desktop
ubuntu.desktop
And x-session starting process in Unity looks like that:
root 964 0.0 0.0 276680 6032 ? SLsl 00:16 0:00 /usr/sbin/lightdm
root 997 3.8 0.9 255676 74708 tty7 Ss+ 00:16 0:15 \_ /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
root 1196 0.0 0.0 230304 6420 ? Sl 00:16 0:00 \_ lightdm --session-child 12 19
user 1442 0.0 0.0 53756 4924 ? Ss 00:16 0:00 \_ /sbin/upstart --user
user 1734 0.0 0.0 39992 280 ? S 00:16 0:00 \_ upstart-udev-bridge --daemon --user
user 1737 0.2 0.0 43932 4332 ? Ss 00:16 0:00 \_ dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-wkfpguwfxw
user 1749 0.0 0.1 93416 8756 ? Ss 00:16 0:00 \_ /usr/lib/x86_64-linux-gnu/hud/window-stack-bridge
So there is no correlation between name of session in /usr/share/xsessions/ and real process cmdline. So pgrep won't find anything in that cases.
This is a very strange situation. I'm on OS X 10.11.6
I have an old tty still hanging around (ttys001) but I don't know how to access it and why its still there. It simply does not have any window on the os x desktop. I'm on ttys000.
$ tty
/dev/ttys000
This means I'm currently on ttys000
$ w
22:01 up 15 days, 7:47, 3 users, load averages: 1.65 1.43 1.45
USER TTY FROM LOGIN# IDLE WHAT
Sidharth console - 30Jul16 15days -
Sidharth s000 - 13:48 - w
Sidharth s001 - Thu13 9:12 -bash
I can understand the login from console (it happens automatically) but where is this s001 (i.e. ttys001) coming from -- I can't switch to it -- I don't see any os x terminal windows corresponding to ttys001.
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
root 30994 30725 30994 0 0 Ss s000 0:00.04 login -pf Sidharth
Sidharth 30995 30994 30995 0 1 S s000 0:00.33 -bash
root 32409 30995 32409 0 1 R+ s000 0:00.01 ps aj
root 26065 1 26065 0 0 Ss+ s001 0:00.04 login -pfl Sidharth /bin/bash -c exec -la bash /bin/bash
Sidharth 26066 26065 26065 0 0 S+ s001 0:00.28 -bash
Sidharth 29465 26066 26065 0 0 S+ s001 0:00.00 -bash
These are the various processes with associated ttys. Again, I can't understand the life of me what 26065, 26066 and 29465 (all associated with 26065) are doing/why are they there.
Some observations: the parent of 30944 is 30725 which is the Mac Terminal application (this makes sense). But equally interesting is that the parent of 26065 (corresponding to login -pfl Sidharth /bin/bash -c exec -la bash /bin/bash is launchd i.e. pid 1)
I've noticed stuff like this before: there is usually an old ttys but its not visible.
Nope I'don't have any additional tabs open in my os x terminal program that could cause this
My question is this: Why is my ttys001 inaccessible? How do I "get to" ttys001
I have a simple script that checks for files to download.
The problem is after some time I can see him multiple times running, started in different times, even though I started him only once:
ps aux | grep _db
root 2804 0.0 0.0 11288 1756 ? S 00:26 0:06 /bin/bash /script/downloader/downloader_db.sh
root 8606 0.0 0.0 11284 872 ? S 12:18 0:00 /bin/bash /script/downloader/downloader_db.sh
root 8649 0.0 0.0 11168 680 pts/0 S 12:18 0:00 /bin/bash /script/downloader/downloader_db.sh
root 11552 0.0 0.0 11272 860 ? S 11:25 0:00 /bin/bash /script/downloader/downloader_db.sh
root 11562 0.0 0.0 11152 672 pts/0 S 11:25 0:00 /bin/bash /script/downloader/downloader_db.sh
root 39150 0.0 0.0 11172 1644 pts/0 S 10:51 0:01 /bin/bash /script/downloader/downloader_db.sh
I started the script with nohup from rc.local:
nohup /script/downloader/downloader_db.sh &> /dev/null &
the script:
#!/bin/bash
while true; do
while IFS=$'\t' read -a line; do
...
sleep 2
done < <(mysql --batch -u${user} -p${password} ${database} -e "${query}" -h ${host})
sleep 10
done
Is anything in the while loop running in the background? It is conceivable that it has the same name as the parent process.
You can check whether some of the processes are subprocesses by running ps wafux and looking at the process tree.
If they are not "related" to each other, you have very likely simply run the script more than once, and the other processes are not finished yet.