Hot answers tagged

19

There's often confusion between process forking and execution. When you do at the prompt of a bash shell. $ sh -c 'exec env ps' The process P1 issuing that $ prompt is currently running bash code. That bash code forks a new process P2 that executes /bin/sh which then executes /usr/bin/env, which then executes /bin/ps. So P2 has in turn executed code of ...


17

The perl function kill(0,$pid) can be used. If the return code is 1 then the PID exists and you're allowed to send a signal to it. If the return code is 0 then you need to check $!. It may be EPERM (permission denied) which means the process exists or ESRCH in which case the process doesn't exist. If your checking code is running as root then you can ...


10

SIGSTOP and SIGKILL are two signals that cannot be caught and handled by a process. SIGTSTP is like SIGSTOP except that it can be caught and handled. The SIGSTOP and SIGTSTP signals stop a process in its tracks, ready for SIGCONT. When you send that process a SIGTERM, the process isn't running and so it cannot run the code to exit. (There are also SIGTTIN ...


8

Your syntax has many problems: remove spaces around "=" when setting a variable wrong: APP_ID = value right: APP_ID=value to run a program and put its output into a variable, you need $(...) (preferred in bash, not available in sh) or backticks `command` (supported in bash and sh). (and remember that when assigning a variable, quotes are not needed, ...


8

In pretty much any shell you're likely to be using, you should be able to press control-Z to suspend the process and then use the bg command to resume it in the background. e.g. $ sleep 1000 ^Z[1] + Stopped sleep 1000 $ bg [1] sleep 1000& $ jobs [1] + Running sleep 1000 $ Note that some jobs (e.g. those that ask ...


7

Yes, a program can know who its parent is. To illustrate, let's create two bash scripts. The first one reports its PID and starts the second script: $ cat s1.sh #!/bin/bash echo s1=$$ bash s2.sh The second script reports its process ID, the PID of its parent, and the command line used to run the parent: $ cat s2.sh #!/bin/bash echo s2=$$ PPID=$PPID ...


7

This is what shell scripts are for; to run several commands while only typing one. The following is a short script that I believe does what you asked for: #!/bin/sh tor & tor_pid=$! polipo & polipo_pid=$! sleep 5 pianobar kill $tor_pid kill $polipo_pid Put that in a file, maybe $HOME/local/bin/startpiano, make it executable with chmod +x $...


5

SIGTERM is just like any other signal in that it can be caught by a process. Receiving the signal will just make the process jump to a special signal handler routine. For SIGTERM the default action will be to terminate the process, but e.g. an editor might like to catch the signal so it can save a draft copy of any open files before dying. If the process is ...


5

I’m 99.9% sure that checking whether /proc/PID exists (and is a directory) is 98% as reliable as the kill 0 technique.  The reason why the 98% isn’t 100% is a point that Stephen Harris touched on (and bounced off) in a comment — namely, the /proc filesystem might not be mounted.  It may be valid to claim that a Linux system without /proc is a damaged, ...


4

1.) Yes, although there is more to it. ttys000 is also a character device sitting in /dev, a user that has permissions to write to the tty group (most users have) can pipe characters into that device and they will appear on the corresponding terminal. ttys* are not real teletypes though, they're emulated ttys, emulated by your (appropriately named) ...


4

On a linux system, you can hide process information by mounting /proc with the appropriate hidepid setting, e.g. mount -o remount /proc -o hidepid=2. However, this will not hide process information from anyone with root access. The better solution is to not include sensitive data on the command line.


3

Better would be to rewrite myapplication so it gets the password via some other mean like stdin. The environ is another option, but that is still visible to processes with the same euid (or euid 0) via /proc/<pid>/environ. If not, on Linux with versions prior to 4.2, you can limit the exposure by making sure the password is not in the first 4096 bytes ...


3

This design has race condition written all over it. You can't be certain that the signals will be delivered in order, or even at all. If you're using Perl that makes it doubly uncertain. Redesign so that there's some confirmation from parent to child that the data got received. Signal delivery order and number is an unreliable thing, asking for a portable ...


3

If you have root access (or sudo ps) then you can display the environment of a process with the e option. Inside here you should be able to see the DISPLAY variable (if it's set). You probably need ww to ensure the output doesn't get truncated. e.g. % ps wwep $$ | tr ' ' '\012' | grep DISPLAY DISPLAY=:0 So my current shell is talking to :0. Many OS's ...


3

This is easiest done with pgrep and pkill: if pgrep process_name 2>/dev/null; then echo "Terminating process_name" pkill process_name fi As user @OrangeDog points out in the comments, there is the theoretical possibility of this shell code failing to terminate the process process_name if the process spawns inbetween the call to pgrep and pkill. In ...


3

I launch my GUI apps mostly with keyboard shortcuts, and for apps I only want one instance of, I point those shortcuts to a wrapper that either gives me the last running instance (and puts that window to the front), or creates a new instance if there isn't one. I have: lonew: #!/bin/sh -e #lastof or new -- try lastof and give me a new instance if it ...


2

put this in your .profile or .bashrc or .zshrc or whichever shell you use: function mycommand { tor & polipo & planobar } And now just run mycommand


2

Handling it with different user accounts may well be the only possible way since processes do not own any files and can therefore not have a disk quota. To make it even clearer, at the very best you could manage a quota for the files currently used, should you develop such a kernel patch, but it would still lose its sense to track the files that were ...


2

PIDs do wrap around in normal usage. That's not a problem at all; the kernel ensures that new PIDs don't collide with existing PIDs. Nothing says that PIDs have to be monotomically increasing; process 12345 could easily fork() and have a child process of 5001. In this scenario, yes, a user could potentially use up all process slots and prevent further ...


1

Another option could be the use of screen or tmux. You can detach your current session and continue your work later, even from another system. The biggest gain is that the session is automatically detached when the line hangs up. With tmux you can even split the screen into panes, switching between them and rearrange the portions with the mouse.


1

If you call su then you will be asked for password, unless you are root. To be able to re-nice with negative values, the process will need: to be root or have capability CAP_SYS_NICE (see What are the different ways to set file permissions etc on gnu/linux and http://man7.org/linux/man-pages/man7/capabilities.7.html )


1

You can use Filtering within top with o (small 'o'). After pressing you see a line for your command (something like add filter #1 (ignoring case) as: [!]FLD?VAL. Just write down, what you want to see: %CPU>30.0. And hit Enter. Tadah! More details can be found with FILTERING in a Window section of man top Older part of the post: ps -eo %cpu,args | awk ...


1

I am not sure why but the above script was not giving any syntax issues but was not giving the process id also though the process was running. I finally changed it as below and it works - APP_ID=$(ps -ef | grep <app_name> | grep java | awk '{print $2}') echo "Instance $APP_ID" if [ -n "${APP_ID}" ]; then echo "Stopping instance $APP_ID" fi ...


1

See man top: When this toggle is Off, tasks that have not used any CPU since the last update will not be displayed. Thus the "CPU usage limit for a process to show up as idle or non-idle" is anything greater than 0.



Only top voted, non community-wiki answers of a minimum length are eligible