Linux pgrep active xsession - bash

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.

Related

When ssh to remote host, how can I know the bash pid running on remote host?

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.

Inaccessible tty still has some bash processes

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

How can I tell many unicorn workers are running on a heroku dyno right now?

I know that you can look in config/unicorn.rb (or the equivalent) and see what those settings are, but I'm wondering specifically how I can tell, right now, how many unicorn workers are running on a given dyno.
I tried to ps aux after running 'heroku run bash' but that didn't give me the actual processes the dyno was running.
If you run:
$ heroku run bash
$ unicorn -c config/unicorn.rb &
$ ps euf
you should get something similar to this:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
u16236 2 0.0 0.0 19444 2024 ? S 20:55 0:00 bash GOOGLE_ANALYTICS_ID=XXX HEROKU_POSTGRESQL_COPPER_URL=postgres://XXX:
u16236 3 19.4 0.3 288716 131568 ? Sl 20:55 0:04 \_ unicorn master -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 5 31.0 0.3 305844 129636 ? Sl 20:55 0:04 | \_ sidekiq 3.2.5 app [0 of 2 busy] GOOGLE_ANALYTICS_
u16236 7 0.0 0.3 288716 124724 ? Sl 20:55 0:00 | \_ unicorn worker[0] -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 10 0.0 0.3 288716 124728 ? Sl 20:55 0:00 | \_ unicorn worker[1] -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 13 0.0 0.3 288716 124728 ? Sl 20:55 0:00 | \_ unicorn worker[2] -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 30 0.0 0.0 15328 1104 ? R+ 20:55 0:00 \_ ps euf GOOGLE_ANALYTICS_ID=XXX DEVISE_PEPPER=XXX
You can see that processes 7, 10, & 13 are my 3 Unicorn workers that are each consuming 30% of total memory.

Ps aux: shows the same script multiple times

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.

change user owner of process on Mac/Linux?

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

Resources