Hot answers tagged

77

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


27

Don't use the name to kill it. Since the calling.sh script is calling the process you later want to kill, just use $! (from man bash): ! Expands to the process ID of the job most recently placed into the background, whether executed as an asynchronous command or using the bg builtin So, if you're calling.sh is like this: called.sh & ## do ...


19

At boot, the kernel adjusts the default pid_max depending on the number of CPUs available. When the number is low, the usual 32768 is selected. Else the calculation is done as follows (showing here a 3.10 kernel to be similar to RHEL but beside some variations it's the same for any recent Linux kernel): include/linux/threads.h: /* * This controls the ...


18

I've had to pick this but out of scripts so many times; it's even more fun when the scripts are called as part of a complex automated schedule. You really shouldn't rely on something like pkill to select which script to kill. Inside of calling.sh you should record the PIDs of the jobs you have started and kill them explicitly by PID. Inside calling.sh: ./...


15

The & command separator will do this for you. Use it carefully and wisely, but here is a simple way to see process relationships: $ sleep 5 & pstree -p $$ [1] 13369 bash(13337)─┬─pstree(13370) └─sleep(13369) The [1] 13369 shows that sleep (which has PID 13369), has been put into the background as Job #1. $$ returns to the shell the ...


15

From the proc documentaton : On 32-bit platforms, 32768 is the maximum value for pid_max. On 64-bit systems, pid_max can be set to any value up to 2^22 (PID_MAX_LIMIT, approximately 4 million). You can see the with cat /proc/sys/kernel/pid_max. You can also query this with sysctl. sudo sysctl -a | grep kernel.pid_max Or: sysctl -n kernel.pid_max ...


14

pkill has a -n flag that will make it only affect the most recently started ("newest") matching process. pkill -n omxiv If the omxiv process is well behaved, there is no need to use -9. There is also a -o flag that will make pkill send a signal to the oldest matching process. Note that using pkill -n will always kill the newest instance. If you have ...


13

Set-up a trap handling SIGINT (Ctrl+C). In your case that would be something like: trap "kill -2 $pid1 $pid2" SIGINT Just place it before the wait command.


10

On Linux, you can use this to get the status of a process with a given PID: ps -o stat= $pid That returns T when a process is stopped. So, assuming you are on a Linux system, you could do something like this: if [ "$(ps -o stat= $pid)" = "T" ]; then echo stopped else echo not stopped fi A full list of process state codes is given in man ps: ...


10

On some Unix-style systems (BSDs and macOS), CtrlT sends SIGINFO to the running process. Some commands handle this directly; otherwise, it’s handled by the kernel, and that’s what produces the output you’re seeing. SIGINFO on GNU Linux (Arch Linux) missing has more on the topic.


9

If you don't want to have any shells in the mix, you can simply use one of the "wrapper" programs that spawn another program to do something with it: /bin/time sleep 60 watch sleep 1 (this one will keep respawning sleep)


8

Yes, you Need to acquire the process id of the process-to-paus (PS), then do $> kill -SIGSTOP <pid> The process will then Show up with Status "T" (PS). To continue do a $> kill -CONT <pid> Good Luck!


8

Use rsync, forget about cp, for this scenario. there are params to limit bandwith, or can be killed/stoped and started later, in a way it will continue, where it left google rsync example/s


7

Those systems that let unprivileged users shut down the system usually only do it for users that are logged in locally, that is, users that have physical access to the machine and could for instance just as well pull the power chord or press the power button/switch. In that case, it's better to let them shut down the system so it can be done gracefully and ...


7

A process ID can be any value represented by the pid_t type, which is specific to your operating system. In practice, it is usually a 32-bit signed integer, which means the maximum process ID would be 2147483647, or approximately 5000 times larger than the process IDs you're observing. The GNU documentation says: Data Type: pid_t The pid_t data type ...


7

either ps -ax | awk '$3 ~ /R|D/ ' or ps -ax | awk '$3 == "R" || $3 == "D" ' note that implicit action is print you can use o option in ps to change or select field you need ps -a -o stat,pid,args | awk '$1 ~ /[DR]/ { $1="" ; print } '


7

If the user has no read permission on an executable script, then trying to run it will fail, unless she has the CAP_DAC_OVERRIDE capability (eg. she's root): $ cat > yup; chmod 100 yup #! /bin/sh echo yup ^D $ ./yup /bin/sh: 0: Can't open ./yup The interpreter (whether failing or successful) will always run as the current user, ignoring any setuid bits ...


6

There is nothing wrong with your terminal or your shell. By default, ps shows processes with the same effective user identifier as the user running it, and associated with the same terminal. This typically results in only two processes showing up: the current shell, and ps itself. If there are any background processes associated with the current terminal, ...


6

The kernel’s memory map on x86-64 is documented in the kernel itself. The kernel maps user-space (for the current process) PTI data structures all the physical memory the kernel’s data structures, in various blocks, with holes for ASLR the kernel itself its modules Having a full mapping of physical memory is convenient, but its relevance is debated ...


5

Too long to put in a comment, so adding as an answer: That's a Java application keeping those files open, so yes, this scenario can be avoided by using a proper programming style and using the ObjectOutputStream object: //create a Serializable List List lNucleotide = Arrays.asList( "adenine", "cytosine", "guanine", "thymine", "sylicine" ); //serialize ...


4

Processes are not intrinsically bound to cores. Each time a process is scheduled for execution it may be executed by any core in its affinity list. If the affinity list has not been set explicitly, a process can be run on any core. You can check or set the affinity set for a process with the taskset command (see man taskset for its documentation). In ...


4

One approach as an ordinary user is an exec wrapper, assuming the programs are run via a PATH search. That is, your wrapper for program #!/bin/sh env > /some/log/file ... (any other desired logging commands here) ... exec /path/to/real/program "$@" must exist first in PATH, so you might have PATH=/some/wrapper/dir:$PATH and then a wrapper program named ...


4

Process IDs are unique. Per the POSIX fork() documentation: DESCRIPTION The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below: The child process shall have a unique process ID. The child process ID also shall not match any ...


4

Alternatively, source the sub-script: one.sh #!/bin/sh echo one.sh: pid is "$$" . ./two.sh echo done with "$0" (the . command is exactly the same as source in bash, but . is more portable) two.sh echo two.sh: pid is "$$" Sample run: $ ./one.sh one.sh: pid is 31290 two.sh: pid is 31290 done with ./one.sh The script two.sh will be run in the same ...


4

...in shell (I am only interested to Bash) scripts I cannot be 100% sure that a PID I stored in a variable still refers to the background process I started, since it might be reused by the kernel for any other process... Correct. The way shells are programmed, as soon as a child process dies, the shell will call wait() on it right away (storing the ...


4

I suspect you were reading this or something very similar (https://elinux.org/Kernel_Timer_Systems): Timer Wheel, Jiffies and HZ (or, the way it was) The original kernel timer system (called the "timer wheel) was based on incrementing a kernel-internal value (jiffies) every timer interrupt. The timer interrupt becomes the default scheduling quantum, ...


4

That's what the faketime command is designed for. For instance: $ time faketime -f '+0 x10' sh -c 'date +%T; sleep 10; date +%T' 13:29:02 13:29:12 faketime -f '+0 x10' sh -c 'date +%T; sleep 10; date +%T' 0.00s user 0.00s system 0% cpu 1.009 total Started that shell with the clock going 10 times as fast as normal (that sleep 10 slept for 1 second). Clock ...


3

Parsing the output of strace: I used the top answer (with my process ID of 28223) ... > sudo strace -p28223 -s9999 -e write ... write(9, "Info\nI\nCare\nabout", 55) = 55 ... To determine that I care about write(9. (The 9 is used below, it's probably a file handle and might be different for your process.) Then I wrote a quick Ruby script to parse and ...


3

There are no global environment variables. They are passed from parent to child. fork does not change the environment variables. exec without the e post-fix does not change the environment variables. exec with e post-fix overrides environment variables. As well as using the e post-fixed execs to change the environment, you can also do: int pid = fork() //...


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