date
int64
1,220B
1,719B
question_description
stringlengths
28
29.9k
accepted_answer
stringlengths
12
26.4k
question_title
stringlengths
14
159
1,289,319,364,000
So, I have some jobs like this: sleep 30 | sleep 30 & The natural way to think would be: kill `jobs -p` But that kills only the first sleep but not the second. Doing this kills both processes: kill %1 But that only kills at most one job if there are a lot of such jobs running. It shouldn't kill processes with the s...
Use this: pids=( $(jobs -p) ) [ -n "$pids" ] && kill -- "${pids[@]/#/-}" jobs -p prints the PID of process group leaders. By providing a negative PID to kill, we kill all the processes belonging to that process group (man 2 kill). "${pids[@]/#/-}" just negates each PID stored in array pids.
How to kill all jobs in bash?
1,289,319,364,000
I'd like to run and configure a process similarly to a daemon from a script. My shell is zsh emulated under Cygwin and the daemon is SFK, a basic FTP server. For what matters here, the script startserv.sh can be drafted as follows: #!/bin/sh read -s -p "Enter Password: " pw user=testuser share=/fshare cmd="sfk ftpse...
Hitting Enter the script ends the process remains in the background. Almost! Actually, the script has already exited by the time you press Enter. However, that's how you can get your prompt back (because your shell prints its $PS1 all over again). The reason why hitting Ctrl + C terminates both of them is because th...
Start a background process from a script and manage it when the script ends
1,289,319,364,000
Often times I find myself in need to have the output in a buffer with all the features (scrolling, searching, shortcuts, ...) and I have grown accustomed to less. However, most of the commands I use generate output continuously. Using less with continuous output doesn't really work the way I expected. For instance: w...
Works OK for me when looking at a file that's being appended to but not when input comes from a pipe (using the F command - control-C works fine then). See discussion at Follow a pipe using less? - this is a known bug/shortcoming in less.
Is there any way to exit “less” follow mode without stopping other processes in pipe?
1,289,319,364,000
this question is a follow-up to: How to suspend and resume processes I have started firefox from a bash session in gnome-terminal. The process tree looks like this: $ ps -e -o pid,ppid,cmd -H 1828 1 gnome-terminal 26677 1828 bash 27980 26677 /bin/sh /usr/lib/firefox-3.6.15/firefox 27985 27980 ...
Shell jobs live in "process groups"; look at the PGRP column in extended ps output. These are used both for job control and to determine who "owns" a terminal (real or pty). POSIX (taken from System V) uses a negative process ID to indicate a process group, since the process group is identified by the first process i...
How to suspend and resume processes like bash does
1,289,319,364,000
This is what I'm running: alexandma@ALEXANDMA-1-MBP ./command.sh & [2] 30374 alexandma@ALEXANDMA-1-MBP [2] + suspended (tty output) ./command.sh I don't want it to start suspended, I want it to keep running in the background. I'm going to be running a bunch of these in a loop, so I need something that will work tha...
It stops because of the reason given: it tries to output to tty. You can try to redirect the output if ./command.sh supports that, or run the command in a tmux or screen window of it's own. E.g. tmux new-window -n "window name" ./command.sh and then view the list of windows created with tmux list-windows and attach ...
When I run `./command.sh &`, the background task is suspended. How can I keep it running?
1,289,319,364,000
Sometimes, some time after I've backgrounded a process with bg in bash, when I press Enter in the same shell to redisplay the prompt (just to check that I'm still in bash when some output from the background process has been displayed), the background process seems to stop spontaneously. If I do bg again the same prob...
This usually happens if the process tries to read from its stdin stream. When the process is in the background, it receives a TTIN signal and is thus frozen (same behavior as a STOP signal). There is also the dual signal TTOU when a background process tries to write to its terminal. Bringing it to the foreground resum...
Why do backgrounded processes sometimes stop spontaneously?
1,289,319,364,000
suspend is a builtin command in Bash. When would you naturally use this command and find it useful?
Let's say you lack both GNU screen and tmux (and X11, and virtual consoles) but want to switch between a login shell and another interactive shell. You would first login on the console, and then start a new shell, temporarily blocking the login shell. To get the login shell back to do some work there, you'd do suspen...
What is a practical example of using the suspend command in Bash?
1,289,319,364,000
Say I start a process in the terminal and it sends output to standard error while it runs. I want to move the process into the background and also silence it at the same time. Is there a way to do this without stopping the process and starting it again using & and > /dev/null 2>&1 ? I'm wondering if there is some co...
Too late. After a process is started, shell has no more control on process file descriptors so you can not silence it by a shell command. You can only try to kill a SIGHUP to the process. If your process handles it correctly, It should detach from controlling tty. Unluckily, many software do not handle it correctly an...
How can I move a process into the background and also silence its output?
1,289,319,364,000
What's the difference between a process group and a job? If I type pr * | lpr then is it both a process group as well a job? What exactly is the difference between a process group ID and a job ID? Edit: I know it appears similar to What is the difference between a job and a process?, but it is slightly different. Als...
A process group is a unix kernel concept. It doesn't come up very often. You can send a signal to all the processes in a group, by calling the kill system call or utility with a negative argument. When a process is created (with fork), it remains in the same process group as its parent. A process can move into another...
Difference between process group and job?
1,289,319,364,000
I have a basic understanding of how to get a job in foreground switch to background and vice-versa but I am trying to come up with a way so that I can run multiple jobs in the background.I tried to put multiple jobs in the background but only one of which was in running state.I want to have a scenario where I can run ...
You can use the & to start multiple background jobs. Example to run sequentially: (command1 ; command2) & Or run multiple jobs in parallel command1 & command2 & This will start multiple jobs running in the background. If you want to keep a job running in the background, once you exit the terminal you can use nohu...
How to run multiple background jobs in linux?
1,289,319,364,000
In Bash, how do you programmatically get the job id of a job started with &? It is possible to start a job in the background with &, and then interact with it using its job id with Bash builtins like fg, bg, kill, etc. For instance, if I start a job like yes > /dev/null & I can then kill it with the following command...
The command jobs prints the currently running background jobs along with their ID: $ for i in {1..3}; do yes > /dev/null & done [1] 3472564 [2] 3472565 [3] 3472566 $ jobs [1] Running yes > /dev/null & [2]- Running yes > /dev/null & [3]+ Running yes > /dev/null & So...
How to programmatically get the job id of a newly backgrounded process in Bash
1,289,319,364,000
I know that the jobs command only shows jobs running in current shell session. Is there a bash code that will show jobs across shell sessions (for example, jobs from another terminal tab) for current user?
It's unclear (to me) what you'd want this global jobs call to do beyond just listing all processes owned by the current user (ps x), but you could filter that listing by terminal and/or by state. List your processes that are: Connected to any terminal: ps x |awk '$2 ~ /pts/' Stopped by job control (Ctrl+z without bg)...
List all jobs in all shell sessions (not just the current shell), by current user
1,289,319,364,000
I use wget command in the background like this wget -bq and it prints Continuing in background, pid 31754. But when I type the command jobs, I don't see my job(although the downloading is not finished).
When using wget with -b or --background it puts itself into the background by disassociating from the current shell (by forking off a child process and terminating the parent). Since it's not the shell that puts it in the background as an asynchronous job, it will not show up as a job when you use jobs. To run wget a...
Why can't I see the "wget" job when I execute it in the background?
1,289,319,364,000
If I begin a process and background it in a terminal window (say ping google.com &), I can kill it using kill %1 (assuming it is job 1). However if I open another terminal window (or tab) the backgrounded process is not listed under jobs and cannot be killed directly using kill. Is it possible to kill this process...
Yes, all you need to know is the process id (PID) of the process. You can find this with the ps command, or the pidof command. kill $(pidof ping) Should work from any other shell. If it doesn't, you can use ps and grep for ping.
How can I kill a job that was initiated in another shell (terminal window or tab)?
1,289,319,364,000
How these process concepts are related together - background, zombie, daemon and without controlling terminal? I feel that they are somehow close, especially through the concept of controlling terminal, but there is still not much info for me to tell a story, like if you need to explain something to a child reading an...
In brief, plus links. zombie a process that has exited/terminated, but whose parent has not yet acknowledged the termination (using the wait() system calls). Dead processes are kept in the process table so that their parent can be informed that their child of the child process exiting, and of their exit status. Usuall...
Background, zombie, daemon and without ctty - are these concepts connected?
1,289,319,364,000
In bash, if I run kill %1, it kills a backgrounded command in the current shell (the most recent one, I believe). Is there an equivalent of this in fish? I haven't been able to find it online in a bit of web searching. I'm not sure if I did it wrong, but $ ps PID TTY TIME CMD 73911 pts/5 00:00:00 fis...
Your command has worked, but because the job is stopped it has not responded to the signal. From your example where it didn't seem to work, try continuing the process with fg or bg, or forcibly terminate the process with kill -SIGKILL %1, and it will exit. kill %1 works immediately in bash and zsh because it is a buil...
kill %1 equivalent in fish
1,289,319,364,000
I am trying to create a function that can run an arbitrary command, interact with the child process (specifics omitted), and then wait for it to exit. If successful, typing run <command> will appear to behave just like a bare <command>. If I weren't interacting with the child process I would simply write: run() { ...
In your example code, Vim gets suspended by the kernel via a SIGTTIN signal as soon as it tries to read from the tty, or possibly set some attributes to it. This is because the interactive shell spawns it in a different process-group without (yet) handing over the tty to that group, that is putting it “in background”....
Run command in background with foreground terminal access
1,289,319,364,000
As an example: I have working shell script which starts up weblogic (which will continue to run) and then do deployment At the end I bring background process back to foreground, so that shell script does not exit (Exited with code 0) #!/bin/bash set -m startWebLogic.sh & # create resources in weblogic # deploy war #...
As far as I understand your question you are looking for wait: #!/bin/bash startWebLogic.sh & # create resources in weblogic # deploy war # ... wait It does not "bring the process to foreground". But it does wait until weblogic returns. So the shell script does not exit until weblogic exits. Which is the effect yo...
Clean way to bring back background process to foreground in shell script
1,289,319,364,000
I have a c program executable or shell script which I want to run very often. If I want to stop/pause or to notify something I will send signal to that process. So every time I have to check the pid of that process and I have to use kill to send a signal. Every time I have to check pid and remembering that upto system...
I don't think you can reserve or assign PIDs. However, you could start your process in a script like this: myprocess & echo "$!" > /tmp/myprocess.pid This creates a "pid file", as some other people have referred to it. You can then fetch that in bash with, e.g., $(</tmp/myprocess.pid) or $(cat /tmp/myprocess.pid). ...
Run a process to particular/dedicated pid only
1,289,319,364,000
tmux and screen let you run different processes (e.g. vim, a bash script, mysql, psql, etc) in different virtual windows. But traditional Unix job control (using CTRL-z, fg, bg, and jobs) seem to give you some of the same functionality. Are there any advantages of multitasking using traditional job control over the ne...
Suppose you've just started a program outside screen. Suddenly you realize you wanted to do something else in that terminal. Ctrl+Z. Screen and tmux introduce a layer of isolation between the application and the terminal. This isn't always a good thing. For example, I find their scrollback a lot less convenient than x...
What are the virtues of multitasking with traditional job control vs Tmux/Screen?
1,541,850,455,000
I'm using a bash script script.sh containing a command cmd, launched in background: #!/bin/bash … cmd & … If I open a terminal emulator and run script.sh, cmd is properly executed in background, as expected. That is, while script.sh has ended, cmd continues to run in background, with PPID 1. But, if I open another te...
The process your cmd is supposed to be run in will be killed by the SIGHUP signal between the fork() and the exec(), and any nohup wrapper or other stuff will have no chance to run and have any effect. (You can check that with strace) Instead of nohup, you should set SIGHUP to SIG_IGN (ignore) in the parent shell befo...
Process killed before being launched in background
1,541,850,455,000
There is no manual entry for ctrl-z or fg. What does "fg" stand for in the context of job control? In other words, typing ctrl-z will suspend the current job drop me back into the shell, and the command "fg" re-activates the suspended job. What does "fg" stand for?
ForeGround. As with other bash built-ins, there is help for it: $ help fg fg: fg [job_spec] Move job to the foreground. Place the job identified by JOB_SPEC in the foreground, making it the current job. If JOB_SPEC is not present, the shell's notion of the current job is used. Exit Status: S...
What does "fg" stand for?
1,541,850,455,000
From gnome-terminal I know the ability to suspend a job with C-z, and then send it to the background. When I close the terminal the process does not end. Where is the job being managed from, or is it lost?
Your background job continues executing until someone tells it to stop by sending it a signal. There are several ways it might die: When the terminal goes away for any reason, it sends a HUP signal (“hangup”, as in modem hangup) to the shell running inside it (more precisely, to the controlling process) and to the pr...
Where do background jobs go?
1,541,850,455,000
I understand that when I run exit it terminates my current shell because exit command run in the same shell. I also understand that when I run exit & then original shell will not terminate because & ensures that the command is run in sub-shell resulting that exit will terminate this sub-shell and return back to origin...
Until I started answering this question, I hadn’t realised that using the & control operator to run a job in the background starts a subshell. Subshells are created when commands are wrapped in parentheses or form part of a pipeline (each command in a pipeline is executed in its own subshell). The Lists of Commands s...
Why is subshell created by background control operator (&) not displayed under pstree
1,541,850,455,000
I am trying to create a fun Terminal Screensaver which consists of the cmatrix package (one that turns terminal in one similar to the movie The Matrix) and xprintidle to determine idle time of the Terminal. I took some help from this Thread answer at SuperUser and using a shell-script similar to it as follows: screens...
The fg job commands are connected to interactive sessions only, and have no meaning in batch mode (scripts). Technically, you could enable them with set -m, but it still would make little (if any) sense, as this only relates to interactive applications where you can send jobs to back by ^Z, but since you don't have th...
Is it possible to use commands like `fg` in a shell-script?
1,541,850,455,000
I have a script which starts a number of background processes and if works fine when called from the cmdline. However the same script is also called during my xsession startup and additionally on some udev events. In both cases the background processes disappear. I had put a sleep 10 into the script and I could see, t...
Protect your processes with nohup: nohup command-name & You can also use this technique if you want to ignore stdout and stderr redirection to nohup.out: command-name & disown
Understanding when background process gets terminated
1,541,850,455,000
I want to run a sequence of command pipelines with pv on each one. Here's an example: for p in 1 2 3 do cat /dev/zero | pv -N $p | dd of=/dev/null & done The actual commands in the pipe don't matter (cat/dd are just an example)... The goal being 4 concurrently running pipelines, each with their own pv output. Howev...
Found that I can do this with xargs and the -P option: josh@subdivisions:/# seq 1 10 | xargs -P 4 -I {} bash -c "dd if=/dev/zero bs=1024 count=10000000 | pv -c -N {} | dd of=/dev/null" 3: 7.35GiB 0:00:29 [ 280MiB/s] [ <=> ...
How can I run multiple pv commands in parallel?
1,541,850,455,000
In Bash 4.X It it possible to do something like: command that expects input & echo some output | %1 Where %1 represents the first backgrounded command?
Once you start: rm -i -- * & rm has been started with whatever stdin was in your shell at the time you invoked that command. If it was the terminal, then rm will typically be suspended (with the SIGTTIN signal) as soon as it tried to read from it (since it's not in the foreground process group of the terminal). If yo...
Pipe command output to input of running/backgrounded command
1,541,850,455,000
I have the following script: suspense_cleanup () { echo "Suspense clean up..." } int_cleanup () { echo "Int clean up..." exit 0 } trap 'suspense_cleanup' SIGTSTP trap 'int_cleanup' SIGINT sleep 600 If I run it and press Ctrl-C, Int clean up... show and it exits. But if I press Ctrl-Z, the ^Z characters are ...
Signals handling on Linux and other UNIX-like systems is a very complex subject with many actors at play: kernel terminal driver, parent -> child process relation, process groups, controlling terminal, shell handling of signals with job control enabled/disabled, signal handlers in individual processes and possibly mor...
How to cleanup on suspense (ctrl-z) in a Bash script?
1,541,850,455,000
I had some stress-testing scripts that were running in parallel and they would hang after finishing and would wait for a RETURN keystroke to exit. After investigating I discovered that it is not peculiar to my scripts but to all sorts of scripts ran on bash and that it depends on the size of the output produced (at le...
The command does not hang. You think that the command is hanging because you don't see the prompt. The prompt is there. You don't see the prompt because it was pushed up by the output of the background process. Pressing enter after the long output of a background process causes the shell to "execute" the empty line an...
why do background jobs hang depending on the size of the output?
1,541,850,455,000
If I run jobs -l on command prompt it shows me the status of running jobs but if I run below file ./tmp.sh #! /bin/bash jobs -l It shows empty output. Why is that and how can I obtain information about a status of a particular job inside of a shell script?
jobs shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell... To see this in action, run #!/bin/bash sleep 60 & jobs -l To see information about "jobs" started before a shell script, inside the script, you need to treat them...
Why does the jobs command not work in shell script?
1,541,850,455,000
I have multiple scripts that detach a process from bash using nohup and &>/dev/null &. My question is, how do I kill the process after completely detaching it from bash. using killall or pidof ScriptName doesn't work.
nohup should only affect the hangup signal. So kill should still work normally. Maybe you are using the wrong pid or process name; compare with pstree -p or ps -ef. If you still suspect nohup, maybe you could try disown instead. $ sleep 1000 & $ jobs -p 13561 $ disown $ jobs -p $ pidof sleep 13561 $ kill 13561 $ pidof...
How do I kill a process after detaching it from bash?
1,541,850,455,000
I understand from Informit article that sessions and process groups are used to terminate descendant processes on exit and send signals to related processes with job control. I believe this information can be extracted at any point using the PPID of every process. Do these concepts exist in place just to have a data s...
Process groups exist primarily to determine which processes started from a terminal can access that terminal. Only processes in the foreground process group may read or write to their controlling terminal; background processes are stopped by a SIGTTIN or SIGTTOU signal. You can send a signal atomically to all the proc...
What is the purpose of abstractions, session, session leader and process groups?
1,541,850,455,000
I have this small script I call prompt-to-run. prompt_acc='' read -p 'run `'"$1"'` ' -i "$1" -e prompt_acc $prompt_acc It lets me create a script that fills in a command for me, but gives me the chance to edit or skip running it without stopping the whole script. I have a different script, which we can call long-r...
The only processing done on the expansion of a variable is word splitting and wildcard expansion. Other shell metacharacters are ignored. If you want the contents of the variable to be executed as if you'd typed the command, use the eval command: eval "$prompt_acc" This will perform all normal shell processing of the...
how can I run a command from an environment variable and have the internal trailing ampersand work?
1,541,850,455,000
I have a master process (run-jobs below) that starts other jobs as its sub-processes. When the master process fails (e.g. database failure), it exits with a non-0 status code, which is good, and can be verified by looking into $? variable (echo $?). However, I'd also like to inspect the exit codes of the sub-processes...
Processes report their exit status to their parent and if their parent is dead to the process of id 1 (init), though with recent versions of Linux (3.4 or above), you can designate another ancestor as the child subreaper for that role (using prctl(PR_SET_CHILD_SUBREAPER)). Actually, after they die, processes become z...
Get the exit code of processes forked from the master process
1,541,850,455,000
I've run into a couple of similar situations where I can break a single-core bound task up into multiple parts and run each part as separate job in bash to parallelize it, but I struggle with collating the returned data back to a single data stream. My naive method so far has to create a temporary folder, track the PI...
My naive method so far has to create a temporary folder, track the PID's, have each thread write to a file with its pid, then once all jobs complete read all pids and merge them into a single file in order of PID's spawned. This is almost exactly what GNU Parallel does. parallel do_stuff ::: job1 job2 job3 ... jobn ...
How to merge data from multiple background jobs back to a single data stream in bash
1,541,850,455,000
I often start a long running command that is bound to either CPU, Disk, RAM or Internet connection. While that is running, I find that I want to run a similar command. Let's say downloading something big. Then I start a shell with wget ...1 and let it run. I could open another shell and run the other wget ...2, but no...
If you have already running wget http://first in foreground you can pause it with CTRL+z and then return it back to work with another command right after it: fg ; wget http://second It should work in most cases. If that way is not acceptable, you should go with lockfile. Or even just to monitor the process via ps (lo...
Queue a task in a running shell
1,541,850,455,000
I'm interested in wrapping a command such that it only runs at most once every X duration; essentially, the same functionality as the lodash throttle function. I'd basically like to be able to run this: throttle 60 -- check-something another-command throttle 60 -- check-something another-command throttle 60 -- check-s...
I'm not aware of anything off-the-shelf, but a wrapper function could do the job. I've implemented one in bash using an associative array: declare -A _throttled=() throttle() { if [ "$#" -lt 2 ] then printf '%s\n' "Usage: throttle timeout command [arg ... ]" >&2 return 1 fi local t=$1 shift if [...
How to wrap a command such that its execution is throttled (that is, it executes at most once every X minutes)
1,541,850,455,000
I don't understand the problem about which the standard shell in Debian (dash) complains: test@debian:~$ sh $ man ls ctrl+Z [1] + Stopped man ls $ jobs [1] + Stopped man ls $ fg %man sh: 3: fg: %man: ambiguous Shouldn't fg %string simply bring the job whose command begins with s...
This looks like a bug; the loop which handles strings in this context doesn't have a valid exit condition: while (1) { if (!jp) goto err; if (match(jp->ps[0].cmd, p)) { if (found) goto err; ...
Job control in dash
1,541,850,455,000
I have a script I'm trying to launch with php ./Script.php & The task goes into the background but it is stopped. When I try to run it with bg it simply stays stopped. $ jobs [1]+ Stopped php ./Script.php $ bg [1]+ php ./Script.php & [1]+ Stopped php ./Script.php $ ps shows the jo...
Almost certainly the command tried to read from the terminal when it was not in the foreground process group. This will cause the kernel to send SIGTTIN to the process, which by default will stop the process. A simple test would be to redirect stdin to /dev/null. If the process runs in the background without being ...
What would stop a task from being run in the background?
1,541,850,455,000
When I gedit files from the command line, it's always locking the terminal, and I'm tired of explicitly commanding a detached process for it. I tried to alias gedit as something like gedit $* & disown, but either that's not the right syntax or you're not allowed to overload executable binary commands with aliases (tri...
That should work: are you sure your .bash_aliases is read? (It's not a standard file, but it might be sourced by your ~/.bashrc. If you're confused about .bashrc and .bash_profile, see Difference between .bashrc and .bash_profile.) There's a bug in your function: it should be editorz () { gedit "$@" & disown } Your...
How can I turn the behavior of `gedit something & disown` into the default behavior when calling gedit from the command line?
1,541,850,455,000
According to the documentation: To prevent the shell from sending the SIGHUP signal to a particular job, it should be removed from the jobs table with the disown builtin or marked to not receive SIGHUP using disown -h. https://www.gnu.org/software/bash/manual/html_node/Signals.html Note the OR in the quote. I ca...
Without -h the job is removed from the table of active jobs, with -h it is not. Everything is in the manual: disown [-ar] [-h] [jobspec ...] (...) If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell rec...
Clarification for Bash documentation on disown builtin option -h
1,541,850,455,000
Last night, before abandoning my computer for the evening, I started a bunch of compiler jobs so they'd be ready in the morning, using make -f alpha.mak &>alpha.out &. When I came back and hit return, I saw the following output: [1] Done make -f alpha.mak &>alpha.out [2]- Done ...
According to the Bash Reference Manual: Job Control: In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a +', and the previous job with a-'.
Background task finished notification syntax
1,541,850,455,000
I need to retrieve the PID of a process piped into another process that together are spawned as a background job in bash. Previously I simply relied on pgrep, but as it turns out there can be a delay of >2s before pgrep is able to find the process: #!/bin/bash cmd1 | cmd2 & pid=$(pgrep cmd1) # emtpy in about 1/10 I f...
When a background job is a pipeline of the form cmd1 | cmd2, it's still a single background job. There's no way to know when cmd1 starts. Each & creates one background job. As soon as cmd & returns, the shell is aware of that background job: cmd & jobs lists cmd. cmd & pid=$! sets pid to the process ID that runs cmd. ...
Reliable way to get PID of piped background process
1,541,850,455,000
I like my background processes to freely write to the tty. stty -tostop is already the default in my zsh (I don't know why, perhaps because of OhMyzsh?): ❯ stty -a |rg tostop isig icanon iexten echo echoe echok -echon...
A process can be sent that SIGTTOU signal (which causes that message), when it makes a TCSETSW or TCSETS ioctl() for instance (like when using the tcsetattr() libc function) to set the tty line discipline settings while not in the foreground process group of a terminal (like when invoked in background from an interac...
zsh: Why do I get suspended background processes even when I have `stty -tostop`?
1,541,850,455,000
I started a for loop in an interactive bash session. For this question we can assume the loop was something like for i in dir/*; do program "$i" done > log The command takes a lot longer than expected and still runs. How can I see the current progress of the running for loop. Non-Solutions: Look at log. Doesn't ...
Wildcards as dir/* always expand in the same order. This feature together with the current value of $i can be used to show a progress information. The current value of $i can be retrieved by looking at the processes running on your system. The following command prints the currently running instance of program and its ...
Show progress of a for loop after it was started
1,541,850,455,000
I have set up an alias for my mutt: alias mutt='$HOME/.mutt/run-notmuch-offlineimap & ; mutt'. Note: Changing my Alias to alias mutt='$HOME/.mutt/run-notmuch-offlineimap 2> /dev/null & ; mutt' or to alias mutt='$HOME/.mutt/run-notmuch-offlineimap 2>&1 >/dev/null & ; mutt' produces the exact same result. The script run...
If you are in zsh then you can change the alias to launch the background process with &! instead of just &. This will immediately disown the process. alias mutt='$HOME/.mutt/run-notmuch-offlineimap &! mutt' If you are in bash then you can use disown after the command to have a similar effect, but you will still get t...
prevent "[1] + done $scriptname" and "[1] 31303" to be shown
1,541,850,455,000
From man zsh, you can see: If you [...] try to exit again, the shell will not warn you a second time; the suspended jobs will be terminated, and the running jobs will be sent a SIGHUP signal, if the HUP option is set. Is there a way to prevent zsh from quitting if a job is in the background?
Looking at the source code, there doesn't seem to be a direct way of doing this. The check for background jobs is performed in zexit, which is called when the main zsh process decides to try exiting, whether from exit, from logout, from an end-of-file or various other circumstances. To cancel the order to exit, stopms...
Prevent Zsh from quitting if job in background
1,541,850,455,000
I have written following function in my bashrc. It checks if a job is running every 5 seconds and sends email when job is finished. function jobcheck () { time=`date` jobstatus=`jobs $1` while [ 1 ]; do if (( ! `echo $jobstatus | grep "Running" | wc -l` )); then "sendEmail command to send email" break;...
You will have to rewrite your function to be able to do that. When you start a background job with &, the shell does indeed keep track of that, and you can indeed figure out more information by using the jobs builtin. However, that information is specific to that instance of the shell; if you run your function with & ...
bash function that sends email after job is finished
1,541,850,455,000
While studying the internals of bash's job control mechanism under Linux I have came across a little problem of understanding. Let's assume the following scenario: script is executed in background user@linux:~# ./script.sh & same script is executed in foreground at the same time user@linux:~# ./script.sh Now the first...
Yes, bash uses waitpid() with WNOHANG in a loop. You can see this in waitchld() in jobs.c: static int waitchld (wpid, block) pid_t wpid; int block; { ... do { /* We don't want to be notified about jobs stopping if job control is not active. XXX - was interactive_shell instead of job_c...
How does bash's job control handle stopped or terminated background jobs?
1,541,850,455,000
Possible Duplicate: How can I disown a running process and associate it to a new screen shell? The problem is that the process is not a job inside of my active shell (as I've logged in from remote because my X-system has crashed), meaning: I guess disown, nohup, CTRL+Z & bg, screen et al. will not work. Motivation...
Depending on the scripting language you use to run the job you could use setpgrp() Perl: setpgrp PID, PGRP to detach the running process from the controlling terminal, so once it starts the controlling terminal can exit without harming the running process. Now from what you are describing you will have the controlli...
How do I detach a process from its parent? [duplicate]
1,484,929,944,000
Job control is probably my favorite thing about Linux. I find myself, often, starting a computationally demanding process that basically renders a computer unusable for up to days at a time, and being thankful that there is always CTRL-Z and fg, in case I need to use that particular computer during that particular tim...
There is no job stack, each job is handled independently. You can do fg; otherbigjob. That will put reallybigjob back to the foreground, then when the fg command stops run otherbigjob. This isn't the same thing as queuing otherbigjob for execution after the first job: if you press Ctrl+Z then otherbigjob starts immedi...
Basic job control: stop a job, add a job onto the stack, and `fg`
1,484,929,944,000
I have a process that is blocking in nature. It is executed first. In order to execute the second process, I moved the first process to the background and executed the second process. Using the wait statement, I am waiting in the terminal. However, it seems that upon exiting the shell (pressing CTRL+C), the first proc...
You could also trap for an interrupt to ensure the process is killed if ctrl+c is pressed #!/bin/sh trap ctrl_c INT ctrl_c () { kill "$bpid" exit } # start process one in background python3 -m http.server 8086 & bpid=$! # start second process firefox "http://localhost:8086/index.html" wait
Call other process after executing a blocking process in shell
1,484,929,944,000
I have a Bash script of a thousand lines each containing an ffmpeg command. I start this with source script and it runs just fine. However, when I try to take control of this script in various ways, things go completely awry: If I do Ctrl + Z to pause the whole thing, only the current ffmpeg command is paused, and th...
Try running the script as a script instead of sourcing it: $ bash <scriptname>
Job control over a Bash script
1,484,929,944,000
Here https://unix.stackexchange.com/a/104825/109539 man says that to stop background process kill + PID must be used. However I can't stop background process using kill +PID, only kill + JOB ID [KPE@home Temp]$ jobs [KPE@home Temp]$ ps PID TTY TIME CMD 13270 pts/0 00:00:00 bash 23257 pts/0 00:00:00 p...
If you C-Z the mc program (=send it a SIGSTP or SIGSTOP = suspend it (will show as "Stopped" in the shell)), it won't be immediately receptive to any more signals (other than SIGKILL, but using that one isn't very nice) until it is resumed. Once you resume it with SIGCONT, it will accept your SIGTERM signal (and the ...
Linux stop background process via kill + PID
1,484,929,944,000
I need to remotely install a program on a Linux computer. I do: ./configure make make install However I seem to get issues when I run ./configure (it's a separate problem) where the configuration screen essentially freezes; it doesn't move past a certain check. I need to stop the configuration so I do Ctrl+z, and tha...
Control+ Z suspends (TSTP/SIGSTOP signal) the most recent foreground process, which returns you back to your shell. From the shell, the bg command sends the suspended process to the background while the fg commands brings it back to foreground. Try Control+C, which sends SIGINT, killing the process. Some software rea...
How to stop ./configure script?
1,484,929,944,000
I went through the top answer to this question: Difference between nohup, disown and &, and I read specfically the following: With disown the job is still connected to the terminal, so if the terminal is destroyed (which can happen if it was a pty, like those created by xterm or SSH, and the controlling program...
For a Standard Shell (bash) (POSIX.1) Start it with &, make it read from something other then the default stdin (==/dev/tty == /dev/stdin == /dev/fd/0) + make it write to something other than the default stdout (==/dev/tty == /dev/stdin == /dev/fd/1) (same for stderr) and make sure the job isn't or doesn't get suspend...
Preventing terminal disconnection from killing a running job in zsh [duplicate]
1,484,929,944,000
I'm currently working on an audit-script for a huge platform. In the main-script we use traps and in one of the traps I ask the user for clean up the files. The script has no output in standard output, so running the script in the bg is obvious. When sending a SIGQUIT to the bg-job it stops, and I have to put it manu...
Is job-control inside a script only for sub-shells/child-processes or can I use it to control the job I started? Yes. It's the parent shell that does the job control, you can't put the child process to foreground from within itself. Edit: You can however still do it like this: Child script: #! /bin/sh ... trap "ki...
Force a job to come in foreground when asked for user input
1,484,929,944,000
If I have the following jobs running in a shell -> % jobs -l [1] 83664 suspended nvim [2] 84330 suspended python [3] 84344 suspended python [4] 84376 suspended nvim [5] - 84701 suspended python [6] + 84715 suspended python How can i return to the nth job, suppose I want to return to job 4, or job ...
To return to job 4, run: fg %4 The command fg tells the shell to move job %4 to the foreground. For more information, run help fg at the command prompt. When you want to suspend the job you are working on, run bg. For more information, run help bg at the command prompt. For more detail than you'd likely want to kno...
Return to a particular job in the jobs list
1,484,929,944,000
Does the following way $ (sleep 123 &) $ jobs $ remove the process group of sleep 123 from bash's job control? What is the difference between the above way and disown then? Note that the sleep 123 process is still in the same process group led by the now disappearing subshell, and in the same process session as the...
Yes, it removes it, so to speak. When you run a (...) subshell from an interactive bash script, a new process group (job) is created, which becomes the foreground process group on the terminal, and in the case where the subshell contains any command terminated by &, (eg. sleep 3600 &) that command will be started in t...
Does ` (sleep 123 &)` remove the process group from bash's job control?
1,484,929,944,000
So I've got an SSH reverse tunnel open, and I'm using tail to pipe the output of the sshd log into awk to detect certain login events and trigger an action. It looks like this: ssh -NR 2222:127.0.0.1:22 server & tail -fn 0 /var/log/auth.log | \ awk '/Invalid user [a-z]+ from 127.0.0.1/ {system("rsync -a source d...
You could suspend only awk and flush the pipe it's reading from under its feet before resuming it. On Linux-based systems and with GNU dd: Where $pid is the PID of awk: kill -s STOP "$pid" to stop it and dd iflag=nonblock if="/proc/$pid/fd/0" > /dev/null; kill -s CONT "$pid" to resume. That's assuming the pipe did n...
Force a process to ignore/discard accumulated input while suspended?
1,484,929,944,000
I went through the Jobs & Signals documentation in Zsh, but some things aren't still clear to me. It says: If the MONITOR option is set, an interactive shell associates a job with each pipeline. What exactly is a pipeline and what is the relationship between a pipeline, a job and a process? Is MONITOR enabled by de...
if you type something like ls -l|grep foo your shell will start two processes (ls and grep). It will (because of the pipe |) also connect them to one pipeline. An interactive shell will also provide job control. This means you can do things like pausing a job or putting it in background. Typing sleep 10& will run a pr...
Pipelines, Jobs and Processes in Zsh
1,484,929,944,000
I'm running an ant (Java build tool) script on CentOS 5.5 that execs another java process. When I run the ant script in the background: ant -f myfile.xml &> foo.out & The forked process' state changes to stop and waits for input. As soon as I bring the process to the foreground it starts again (no input required on ...
I found the answer. A little googling brought up this page: http://ant.apache.org/manual/running.html#background Looks like ant immediately tries to read from standard input, which causes the background process to suspend
ant script stops, waiting for input when run in background
1,484,929,944,000
I'm writing a shell wrapper script that is supposed to act as a pager (receiving input on stdin and performing output on a tty). This wrapper prepares environment and command lines, then launches two processes in a pipeline: first one is a non-interactive filter and the last one is the actual pager that needs to contr...
You can also do: #!/bin/zsh - col -bx | exec bat "${bat_args[@]}" --paging=always Or: #!/bin/ksh - col -bx | exec bat "${bat_args[@]}" --paging=always (not with the pdksh-derived implementations of ksh) #!/bin/bash - shopt -s lastpipe col -bx | exec bat "${bat_args[@]}" --paging=always In the end functionally equi...
Is there a way to `exec` a pipeline or exit shell after launching a pipeline?
1,484,929,944,000
Until recently I was pretty satisfied with how the fg and jobs command worked in my zsh, i.e.: just fg -> foreground the most recently backgrounded job again jobs -> display command name (incl. args) and perhaps even PID (don't recall) After the latest Fedora 33 updates these zsh behaviors changed in a (for me) pret...
It's caused by Fedora started defining system-wide vim aliases that start vim in a sub-shell. Since this seems to break stuff left and right those aliases are now being rolled back: https://bugzilla.redhat.com/show_bug.cgi?id=1918575
Configure zsh jobs output and fg behavior
1,484,929,944,000
I have this Bash script named as s in current directory: #!/bin/bash pipe_test() { ( set -m; ( $1 ); set +m ) | ( $2 ) } pipe_test "$1" "$2" If I call e.g. ./s yes less the script gets stopped. (Similar thing happens if I use any other pager I tried instead of less, i.e. more and mos...
The reason why that happens is because enabling job control (set -m) brings along not just process grouping, but also the machinery for handling "foreground" and "background" jobs. This "machinery" implies that each command run in turn while job control is enabled becomes the foreground process group. Therefore, in sh...
less stops my script; why is that and how to avoid?
1,484,929,944,000
I have the following code in my ~/.zshrc: nv() ( if vim --serverlist | grep -q VIM; then if [[ $# -eq 0 ]]; then vim elif [[ $1 == -b ]]; then shift 1 IFS=' ' vim --remote "$@" vim --remote-send ":argdo setl binary ft=xxd<cr>" vim --remote-send ":argdo %!xxd<cr><cr>" e...
The problem is starting a background job from a trap. The job seems to get “lost” sometimes. Changing vim to vim & makes the job be retained sometimes, so there may be a race condition. You could avoid this by not starting the job from a trap. Set a flag in the trap, and fire up vim outside the trap, in the precmd hoo...
How to start Vim from a trap and still be able to resume it after suspending it?
1,484,929,944,000
This is the output: [USER@SERVER ~] ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.037 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.024 ms 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.030 ms 64 bytes fr...
You use bg normally to run programs in the background, which has no console interaction, like most program with a graphical user interface. Example: You wanted to run xterm & but forgot the & to run the terminal emulator in the background. So you stop the (blocking) foreground xterm process with Ctrl-Z and continue i...
What is the real-world use of the bg command?
1,484,929,944,000
There is list of jobs running. pdf is opened image is opened text file opened It's all in fg/bg . Is there any option to close the particular job forcefully? Also, I want to know if the jobs can be closed with the help of a command.
As long as the jobs were all started from your current shell: use 'jobs' to get a list of backgrounded jobs. Each will have a numeric identifier, starting from '1'. Then you can bring the job to the foreground with fg %1, send it to the background if it's paused with bg %1, or kill it with kill %1 (use the correct n...
how to close the jobs one by one? is there any options?
1,484,929,944,000
I have two shell-scripts, say client.sh and server.sh, which has to work simultaneously and give some useful output in watch-way. And I am able to use only one terminal. So I should switch between them to see what's happening but without stopping them (Ctrl+Z). I can't figure out how to do this. When I run ./server.sh...
Use screen: $ screen -S my-job This will start a new screen session named "my-job" and connect to it. $ ./server.sh This will start your server.sh script on the first (default) terminal attached to the screen Session. Now press Ctrl-A followed by Ctrl-C. This will Create a new terminal and switch to it. Now you ca...
How to use one terminal with multiple interactive jobs without stopping them?
1,484,929,944,000
Here is the scenario: Let's say that I log into my server via ssh and start an emacs or vi (or whatever other program) session. Then my ssh connection disconnects. Is there a way for me to reconnect to those programs via a new ssh session. In other words, when I log back into my server through a new ssh session?. In ...
You can use screen Suppose you have logged in using SSH, then simply run following command to create screen session called 'mysession' screen -S mysession in case your connection disconnected, then you can simply attach your session using: screen -x mysession Check this link for more information about screen
How to resume a program when logged in with different session
1,484,929,944,000
My question has nothing to do with WCE (wait and cooperative exit). Assuming i have a script launched in an interactive shell (bash) as a foreground job: #! /bin/bash # script name: foreback.sh sleep 100m & # child process in bg sleep 200m & # ditto sleep 300m & # ditto sleep 7000m # child process in fg exit...
Non-interactive shells don't do job control by default, so all processes run by a script are in the same process group as the process that executed the shell. That process group will have been made the foreground process group of the terminal or not by the interactive shell starting the script depending on whether the...
How does bash recognize background child processes launched by a foreground process (script) on receiving Ctrl-C
1,484,929,944,000
I'm on OpenSUSE 12.1, so no tmux, and we're not allowed to install anything - wget is too old to download a binary as well. Often I and other users have to run long scripts that take several hours, and our SSH client will crash in the middle. I'm aware that this is a bad practice but my opinion isn't valued. What's a...
One option would be screen, if it is available. (You mentioned tmux, but not screen) Another option would be to run the script with "nohup" which will disassociate it from your shell. You would then need to use its pid to monitor it. Redirecting the output to files would also be recommended.
What's the best way to run a long script without the SSH client crashing?
1,500,454,082,000
I was trying to demonstrate job control to a friend, when I ran into an unexpected behaviour. For certain commands, kill works with the job number, but not with the process ID. An example of the bahaviour I expected: user@host:~$ sleep 1h & [1] 4518 user@host:~$ sleep 2h & [2] 4519 user@host:~$ kill %2 [2]+ Terminate...
What the kill builtin really does in these circumstances is not documented in the Bourne Again shell's manual, but it is documented in the Z shell and Korn shell manuals: Korn shell: If the signal being sent is TERM (terminate) or HUP (hangup), then the job or process will be sent a CONT (continue) signal if it is st...
why does kill work differently on job numbers than PIDs?
1,500,454,082,000
Which signal will be sent to the running process after sending the Ctrlc after 500ms of Ctrlz? I have tried to give the Ctrlc after Ctrlz but I didn't get the exact answers for this.
Ctrl+C will send SIGINT to the foreground process group. As you have backgrounded the process beforehand by Ctrl+Z, Ctrl+C won't give you the desired result expectedly.
Result of combination of Ctrl+c and then Ctrl+z in shell
1,500,454,082,000
As far as I've seen, pressing Ctrl-Z on any terminal multiplexer, or trying to start them in the background, does nothing or crashes. I know that, in a sense, terminal multiplexers are a "replacement" for job control, and usually, they have their own mechanisms for suspending and resuming. Still, I was wondering if I ...
Do you want to suspend a job inside a screen window? Just use Ctrl z inside the screen window (as usual). This doesn't suspend screen, though. Do you want to suspend screen itself? Use Ctrl az inside any screen window. But notice that although this suspends the user-facing part of the screen application, it doesn't...
Does any terminal multiplexer (screen, tmux, zellij) support job suspension (Ctrl-Z) in Bash?
1,500,454,082,000
In an X session, I can follow these steps: Open a terminal emulator (Xterm). Bash reads .bashrc and becomes interactive. The command prompt is waiting for commands. Enter vim 'my|file*' '!another file&'. Vim starts, with my|file* and !another file& to be edited. Press CTRL-Z. Vim becomes a suspended job, the B...
I could think of a couple of methods, and I think the first one is less hackish on Bash mainly because it seems (to me) to have quirks that can be more easily handled. However, as that may also be a matter of taste, I'm covering both. Method one The "pre-quoting" way It consists in making your script expand its $@ arr...
Programmaticaly open new terminal with Bash and run commands, keeping job-control
1,500,454,082,000
I want to start a web server via Python. When this succeeds, I want to open the page in the default browser (on macOS, you can do this with the open command), and after that, I want to resume the previous script again. This does not work: #!/usr/bin/env bash cd wwwroot python3 -m http.server & open http://0.0.0.0:8000...
After some experimentation, it seems that $! is the variable you want. Example: (sleep 1 && open http://0.0.0.0:8000) & disown $! python3 -m http.server The sleep makes sure (well, very likely at least) the server is up-and-running before the open is executed. However, there's still the potential for failure if pytho...
How to start a job, do something different, and resume it again
1,500,454,082,000
When I do ( sleep 1; read x ; echo x=$x; echo done ) & then with the default terminal settings, the job gets stopped by SIGTTIN. If I do ( ( sleep 1; read x ; echo x=$x; echo done ) & ) the read syscall inside read gets EOF (returns with 0)` and no stopping by SITTIN happens. What is the explanation for these behav...
That's because in the second case the backgrounded command will be run in a subshell, and as there's no job control in subshells, the background mode will be faked by redirecting the input from /dev/null and ignoring the SIGINT and SIGQUIT signals. See also these answers: Background process of subshell strange behavio...
Behavior of directly vs indirectly backgrounded children on read
1,500,454,082,000
I am developing a web application in Phoenix and I also just started discovering Unix process management. I put my app in background, like this: vagrant@dev:/srv/my_app$ iex -S mix phoenix.server & [1] 8726 Then I'd like to cd to another directory and do some other work in the main prompt. However, as soon as I do t...
Your shell didn't stop, the progress you sent to the background did (the iex process). If you hit "Enter" you'll get a shell prompt back.
How do I keep a prompt running in background? [duplicate]
1,500,454,082,000
Background I work at a research institute, and have for a long time used the batch command to submit job queues to our machines. However, a recent update changed the batch command to be POSIX-compliant, the result of which is it doesn't take input files anymore. Thus, I would have to manually enter each command at the...
expect (and pexpect) were designed to automate interaction with programs that want "interactive" input. expect allows one to automate starting a program, waiting for its prompt, sending a response, waiting for another prompt, etc. Here is a simple example of an expect script that starts batch and creates a job for it...
Replacement queue now that batch doesn't accept input files?
1,500,454,082,000
I have a shell script that initiates a long, resource-intensive command on several different machines. In order to execute the script on each machine in parallel, I have an ampersand symbol after each remote command in the for-loop. initialize-cluster.sh: while read host; do ssh -f "$host" \"/home/user/allocate.s...
Nothing in this script should prevent execution of the next command or return to the shell prompt. What could be giving the impression that the prompt is gone is the output of the remote scripts, which would arrive after return to the shell. To avoid that you could redirect stdout/stderr to some log file. Since the re...
Running commands after ampersand symbol & in a remote ssh session
1,500,454,082,000
Is is possible in bash - or other sh-derivative shell - to run in the foreground from command-line a list of commands that have their own variable scope (so any values assigned to variables in that scope will not known outside that scope), but also - if spawning a background command - that background command still be ...
In zsh, you can use an anonymous function, but you still need to declare variables as local. For instance: $ () { local t; for t in 100 200 300; do sleep $t & done; } [2] 4186 [3] 4187 [4] 4188 $ jobs [2] running sleep $t [3] - running sleep $t [4] + running sleep $t $ typeset -p t typeset: no such varia...
Separate variable scope spawning commands under job control
1,500,454,082,000
I'm working with some tail -f path/to/my/log/file | grep pattern& and I need to kill the process as quick as possible. With classic kill {tail PID}, tail still displays its buffer and it takes around 12 second (on my setup) to get tail completely silent. However, it's much faster when I kill it with kill %{job id} (sl...
When you kill the job with kill %6, you kill the tail and kill grep too. tail -f /var/log/mintupdate.log|grep ez& [6] 3368377 If you kill 3368377, you kill just the grep process. 3368376 pts/6 S 0:00 tail -f /var/log/mintupdate.log 3368377 pts/6 S 0:00 grep --color=auto ez Of course it caused to kill...
"kill %{job id}" vs "kill {job pid}"
1,500,454,082,000
I'm running a long-running pipeline from bash, in the background: find / -size +500M -name '*.txt' -mtime +90 | xargs -n1 gzip -v9 & The 2nd stage of the pipeline takes a long time to complete (hours) since there are several big+old files. In contrast, the 1st part of the pipeline completes immediately, and since ...
FWIW, I can reproduce your case with: rhel8$ /bin/jobs(){ jobs -l; } rhel8$ sleep 1 | sleep 3600 & [1] 2611 rhel8$ sleep 2 rhel8$ jobs [1]+ Running sleep 1 | sleep 3600 & rhel8$ /bin/jobs [1]+ 2610 Running sleep 1 2611 Running | sleep 3600 & rhel8$ pgrep 2610 ...
'jobs' shows a no longer existing process as running
1,500,454,082,000
I'm trying to use some functions in a bash script to simplify calling some child processes. I want to decide at the call site whether or not to run the process in the background as a job, or in the foreground, and later kill the child process when necessary. Unfortunately, as far as I can tell, the pid I get with $! d...
outer & creates a subshell. $! gives the PID of this subshell. tail -f /dev/null is a child of this subshell so it has a different PID. But you can do exec tail -f /dev/null instead. Then the kill hits the tail. Another possibility is to use /bin/kill instead of the shell builtin. With a negative number as argument y...
Killing jobs started within functions
1,500,454,082,000
What is the difference between running following commands on terminal? command1 for i in {1..3}; do ./script.sh >& log.$i & done and command2 for i in {1..3}; do ./script.sh >& log.$i & done & Running the first command shows three job IDs on the screen and I can type the next command on ther terminal. The second ...
The first one for i in {1..3}; do ./script.sh >& log.$i & done runs in the current shell. Each iteration of the loop runs the script.sh script as a job of the current shell, and so you can see them so. The second one for i in {1..3}; do ./script.sh >& log.$i & done & first starts a subshell that controls t...
Ampersand after for loop on shell scripts
1,500,454,082,000
I wanted to do a test for the Job Control Commands. So, I ran a cat command and then made it a background job using the bgcommand after stopping it with Ctrl +Z. Now I wanted to first terminate that background process, so I used the command %kill-2%2 as the Process ID was [2] but it gave me an error saying "No such...
The command should be kill -2 %2 with proper spacing. The % sign at the beginning of your line is probably just the prompt they are using (PS1).
Can't terminate / suspend a background job
1,500,454,082,000
One can launch GUI programs, for example, gv or xpdf from vifm in background in vifm's command line: :!gv %f & However, if gv is launched by pressing Enter on a file like aPSfile.ps in vifm, it blocks the vifm. Is it possible to run it in the background as well when it is launched this way? The following setup in vi...
The vifm documentation explicitly covers this requirement: :filet[ype] pat1,pat2,... [{descr}]def_prog[ &],[{descr}]prog2[ &],... Space followed by an ampersand as two last characters of a command means running of the command in the background. I have filetype *.pdf apvlv & in my .vifm/vifmrc and it backgrounds any .p...
Is it possible to disconnect a GUI program launched within vifm from vifm?
1,500,454,082,000
I want to understand a little bit better, what a background process is. The question came to live as a result of reading this line of code: /usr/sbin/rsyslogd -niNONE & Source The documentations says: -i pid file Specify an alternative pid file instead of the default one. This option must be use...
The Unix concept of a background process is part of job control. Job control is the organization of processes around a terminal session. Daemons normally don't run in a terminal session, therefore they are not background processes in the Unix job control sense, only in the general computing sense of belonging to the m...
What exactly does it mean to run a process in the "background"?
1,500,454,082,000
I have many working jobs running on different consoles. They almost occupied all the CPU usage, which caused me hard to control the system ( very slow response time ) Is there any way to pause these consoles? or any other ways? #update I am actually building Yocto in many different consoles, it seems hard to adjust a ...
There are several ways to pause a process: Send a SIGSTOP to the the process to freeze it (SIGCONT to unfreeze). You can also hit Ctrl+S (Ctrl+Q) to send these signals to an active process. But if it is in background, you would have to use kill or its variations. Use nice to set priorities to processes. By default, ...
Possible to pause console operation?
1,500,454,082,000
In Linux, you can do the following: kill 1 (or kill %1) Which means "close the processes in job number 1". And you can do the following: kill 1234 Which means "send the SIGTERM signal to the process with PID 1234". Are these two kill commands the same command, or are they two different commands?
I’m not sure you can do kill 1 (or rather, you can try, but you won’t be allowed to, unless your root, and then you’re in for a surprise). 1 here always refers to the process with id 1, which is usually init (or some variant thereof). To actually answer your question, if you’re in a shell which supports job control, k...
Is the "kill" command for job control the same as the "kill" command to send a signal to a process?
1,500,454,082,000
Is it possible to know if a script executed inside a screen session finished or not? The script can be seen by typing jobs and I would like to terminate the screen session when the script is finished. How can I do so or can I have the screen terminate itself after the script successfully executes? It is not convenient...
You just chain the commands 1. your_script and 2. kill this screen session Let foo.sh be your script. The command to kill a screen session is kill. You issue commands to a screen session with screen -X, making screen -X kill the shell/bash command to kill the screen session you are currently in. You would screen -S th...
How to know when a job in screen finishes?
1,500,454,082,000
Need an at command timestamp that run some command at given day monthly, like for example, every day 15, as would follow: $ at every 15 day So that on every day 15, it would run some command. How would I set it?
As pointed out in the comments, cron is the right tool to do so. at is used to run a command at a specified time and date but only once. Just add this line to /etc/crontab: 0 7 15 * * youruser /path/to/somecommand This runs the specified command at 7:00 AM every 15th of the month. For more information, see t...
Need an "at" Command Timestamp That Runs Command Monthly at Given Day
1,500,454,082,000
According to this site I set up cron to execute a script for me, first just trying to get it to work with cat before doing the actual work I need to do (actual work will need root priviliges so I did everything as root to make my life easier later): me> sudo su root> crontab -e Edited the file as follows, leaving a b...
Your crontab is running at 1st minute of every hour. To run every minute you have to configure like this. * * * * * cat /home/me/source.txt 1> /home/me/destination.txt
cron doesn't execute scripts after setting it up
1,500,454,082,000
I was reading a book about Linux. It states that to bring a process to foreground, use the fg command and a percent sign (%) followed by the job number. I did some testing and found that it works as expected. But I also found that I can use a simple number as the jobspec, like fg 3 (instead of fg %3), which can bring ...
Bash seems to accept fg 3 etc., but I'm not sure the documentation is too explicit about that. The description for fg just says it takes a "jobspec", and their description says "The character ‘%’ introduces a job specification (jobspec)." and the % seems included in all the examples. The other shells I tried (Dash, ks...
Is a pure number like "3" a valid jobspec?
1,500,454,082,000
I have a script (not written by me, I cannot modify it) that has to run for days, that sometimes fails (exits with an error). In this case all I have to do is just reboot the server (there is no better solution for now), and restart the script. Currently I do this: log in via SSH screen -S job ./myscript.sh to star...
First, I would try to put that script in a container. this would remove some dependencies from the host itself, and allow automatic restart. Solution using docker and docker-compose This approach requires docker and docker compose. If you have Ubuntu, you can istall them via sudo apt install docker.io docker-compose. ...
Reboot and relaunch a script if error
1,500,454,082,000
I run the command (Xorg & sleep 3; xeyes) & to test Xorg, and group it into a single subshell background job to make management easy. This works properly, and opens xeyes in the new Xorg session after 3 seconds. Upon running the command, I'll get an output such as the following: [1] 635 After running ps -ef to check ...
If you want to observe job control, you'd want to use the -j option to ps which will list the process group id and session id. Here, I see: [...] UID PID PPID PGID SID C STIME TTY TIME CMD chazelas 6805 4172 6805 6805 0 06:47 pts/7 00:00:00 /bin/zsh chazelas 6825 6805 6825 6805 0 06:4...
Subshell Job Clarification
1,391,717,026,000
I've got two files _jeter3.txt and _jeter1.txt I've checked they are both sorted on the 20th column using sort -c sort -t ' ' -c -k20,20 _jeter3.txt sort -t ' ' -c -k20,20 _jeter1.txt #no errors but there is an error when I want to join both files it says that the second file is not sorted: join -t ' ' -1 2...
I got the same error with Ubuntu 11.04, with sort and join both in version (GNU coreutils) 8.5. They are clearly incompatible. In fact the sort command seems bugged: there is no difference with or without the -f (--ignore-case) option. When sorting, aaB is always before aBa. Non alphanumeric characters seems also alw...
join : "File 2 not in sorted order"
1,391,717,026,000
File1.txt id No gi|371443199|gb|JH556661.1| 7907290 gi|371443198|gb|JH556662.1| 7573913 gi|371443197|gb|JH556663.1| 7384412 gi|371440577|gb|JH559283.1| 6931777 File2.txt id P R S gi|367088741|gb|AGAJ01056324.1| 5 5 0...
join works great: $ join <(sort File1.txt) <(sort File2.txt) | column -t | tac id No P R S gi|371443198|gb|JH556662.1| 7573913 2 2 0 gi|371440577|gb|JH559283.1| 6931777 21 19 2 ps. does ouput column order matter? if yes use: $ join <(sort 1) <(sort 2) | tac | awk '{pri...
Join two files with matching columns