Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
78

These are indeed the process states. Processes states that ps indicates are: D Uninterruptible sleep (usually IO) R Running or runnable (on run queue) S Interruptible sleep (waiting for an event to complete) T Stopped, either by a job control signal or because it is being traced. W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z ...


78

It should always be OK to do kill -9, just like it should always be OK to shutdown by pulling the power cable. It may be anti-social, and leave some recovery to do, but it ought to work, and is a power tool for the impatient. I say this as someone who will try plain kill (15) first, because it does give a program a chance to do some cleanup -- perhaps ...


78

You can pause execution of a process by sending it a SIGSTOP signal and then later resume it by sending it a SIGCONT. Assuming your workload is a single process (doesn't fork helpers running in background), you can use something like this: # start copy in background, store pid cp src dst & echo "$!" >/var/run/bigcopy.pid Then when busy time starts, ...


76

Instead of suspending the process, you could also give it lower priority: renice 19 "$pid" will give it the lowest priority (highest niceness), so that process will yield the CPU to other processes that need it most of the time. On Linux, the same can be done with I/O with ionice: ionice -c idle -p "$pid" Will put the process in the "idle" class, so ...


72

Sending kill -9 to a process doesn't require the process' cooperation (like handling a signal), it just kills it off. You're presuming that because some signals can be caught and ignored they all involve cooperation. But as per man 2 signal, "the signals SIGKILL and SIGSTOP cannot be caught or ignored". SIGTERM can be caught, which is why plain kill is ...


63

Yes. Try xprop and you are looking for the value of _NET_WM_PID: xprop _NET_WM_PID | cut -d' ' -f3 {click on window}


57

It's to simplify the interface. The alternative to fork and exec would be something like Windows' CreateProcess function. Notice how many parameters CreateProcess has, and many of them are structs with even more parameters. This is because everything you might want to control about the new process has to be passed to CreateProcess. In fact, CreateProcess ...


49

This was implemented to the Linux kernel 3.4 as a flag of the system call prctl(). From the prctl(2) manpage: [...] A subreaper fulfills the role of init(1) for its descendant processes. Upon termination of a process that is orphaned (i.e., its immediate parent has already terminated) and marked as having a subreaper, the nearest still living ...


46

The pgrep and pkill utilities were introduced in Sun's Solaris 7 and, as g33klord noted, they take a pattern as argument which is matched against the names of running processes. While pgrep merely prints a list of matching processes, pkill will send the specified signal (or SIGTERM by default) to the processes. The common options and semantics between pgrep ...


39

I use kill -9 in much the same way that I throw kitchen implements in the dishwasher: if a kitchen implement is ruined by the dishwasher then I don't want it. The same goes for most programs (even databases): if I can't kill them without things going haywire, I don't really want to use them. (And if you happen to use one of these non-databases that ...


31

That is certainly not trivial task that can't be done in userspace. Fortunately, it is possible to do on Linux, using cgroup mechanizm and its blkio controller. Setting up cgroup is somehow distribution specific as it may already be mounted or even used somewhere. Here's general idea, however (assuming you have proper kernel configuration): mount -t tmpfs ...


30

Given that google killed chrome://memory in March 2016, I am now using smem: # detailed output, in kB apparently smem -t -P chrom # just the total PSS, with automatic unit: smem -t -k -c pss -P chrom | tail -n 1 to be more accurate replace chrom by full path e.g. /opt/google/chrome or /usr/lib64/chromium-browser this works the same for multiprocess firefox ...


26

You can use pkill: pkill httpd You may also want to use process substitution(although this isn't as clear): kill $(pgrep command) And you may want to use xargs: pgrep command | xargs kill


26

You have a kernel option configuration where a CPU won't be used by the OS, it is called isolcpus. isolcpus — Isolate CPUs from the kernel scheduler. Synopsis isolcpus= cpu_number [, cpu_number ,...] Description Remove the specified CPUs, as defined by the cpu_number values, from the general kernel SMP balancing and scheduler algroithms. ...


25

Python imports a large number of files at startup: % python -c 'import sys; print len(sys.modules)' 39 Each of these requires an even greater number of attempts at opening a Python file, because there are many ways to define a module: % python -vv -c 'pass' # installing zipimport hook import zipimport # builtin # installed zipimport hook # trying site.so ...


25

Process tree While the process is running try to use ps with the f option to see the process hierarchy: ps axuf Then you should get a tree of processes, meaning you should see what the parent process of the gzip is. If gzip is a direct descendant of init then probably its parent has exited already, as it's very unlikely that init would create the gzip ...


22

xprop will return a window's PID. You can filter the verbose output using awk: xprop | awk '/PID/ {print $3}'


22

ionice from the util-linux does something similar to what you want. It doesn't set absolute IO limits, it sets IO priority and 'niceness' - similar to what nice does for a process' CPU priority. From the man page: ionice - set or get process I/O scheduling class and priority DESCRIPTION This program sets or gets the I/O scheduling class and priority ...


21

In Unix, most editors work by creating a new temporary file containing the edited contents. When the edited file is saved, the original file is deleted and the temporary file renamed to the original name. (There are, of course, various safeguards to prevent dataloss.) This is, for example, the style used by sed or perl when invoked with the -i ("in-place") ...


20

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 ...


18

Yes on both counts. Many processes are short lived. They get a PID, run, finish, and the PID disappears from the process table. Processes sometimes only live for a fraction of a second! Often when programs start they run numerous commands as part of checking the system and initializing their environment. The maximum PID number depends on the system and ...


18

bash goes a long way to make sure it reads commands just before executing them. For instance in: cmd1 cmd2 The shell will read the script by blocks, so likely read both commands, interpret the first one and then seek back to the end of cmd1 in the script and read the script again to read cmd2 and execute it. You can easily verify it: $ cat a echo foo | ...


18

vi-vi-vi is of the devil. You must kill it with fire. Or SIGKILL: kill -KILL %1 The builtin kills are kind enough to send SIGCONT to suspended processes so that you don't have to do it yourself, but that won't help if the process blocks the signal you're sending or if handling the signal causes the processes to become suspended again (if a background ...


17

Try iftop for network and pidstat from sysstat. Both are probably an easy install (apt-get, etc) for the distribution of your choice.


17

What does a niceness of (-) indicate? Notice those also have a PRI score of -100; this indicates the process is scheduled as a realtime process. Realtime processes do not use nice scores and always have a higher priority than normal ones, but still differ with respect to one another. You can view details per process with the chrt command (e.g. chrt -p 3). ...


17

The commands within each group run in parallel, and the groups run sequentially, each group of parallel commands waiting for the previous group to finish before starting execution. The following is a working example: Assume 3 groups of commands as in the code below. In each group the three commands are started in the background with &. The 3 commands ...


17

You can use killall as well, e.g. killall firefox to send SIGTERM to all firefox processes.


16

From the ps manpage: Processes marked <defunct> are dead processes (so-called "zombies") that remain because their parent has not destroyed them properly. These processes will be destroyed by init(8) if the parent process exits.


16

If you type command 1 & command 2 this is equal to command 1 & command 2 i.e. this will run the first command in background and then runs the second command in foreground. Especially this means, that your echo "done" is printed after command 2 finished even if command 1 is still running. You probably want command 1 & command 2 & wait ...


15

As I understand it, a zombie process has died but still exists as a placeholder in the process table until its parent (or init if the zombie is itself an orphan) checks its exit status. Correct. And my understanding of orphan processes is they are processes that are still alive and running but whose parent has died. Correct. Since a zombie is already ...


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