Tag Info

Hot answers tagged

21

By default the terminal will run the program in the foreground, so you won't end up back at the shell until the program has finished. This is useful for programs that read from stdin and/or write to stdout -- you generally don't want many of them running at once. If you want a program to run in the background, you can start it like this: $ lxpanel & ...


14

From Bash's man page: ! Expands to the process ID of the most recently executed background (asynchronous) command. So $! would contain the process ID (PID) of the last job that was backgrounded. Example $ sleep 100 & [1] 18813 $ echo "$!" 18813 References bash man page


14

There are many ways to go about this. Method #1 - ps You can use the ps command to find the process ID for this process and then use the PID to kill the process. Example $ ps -eaf | grep [w]get saml 1713 1709 0 Dec10 pts/0 00:00:00 wget ... $ kill 1713 Method #2 - pgrep You can also find the process ID using pgrep. Example $ pgrep wget ...


13

$! contains the process ID of the most recently executed background pipeline. From man bash: Special Parameters The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed. ... ! - Expands to the process ID of the most recently executed background (asynchronous) ...


10

Put \j in your prompt. From the bash manual: \j The number of jobs currently managed by the shell Just remember that prompts do go stale and jobs can finish at any time, so if you have left the terminal idle, you'll want to redisplay the prompt. At the cost of requiring an extra process just to print your prompt, you can make the \j only ...


10

The easiest way is to run fg to bring it to the foreground: $ 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: Status of command placed in foreground, or ...


10

You can use jobs to list the suspended process. Take the example. Start with a process: $ sleep 3000 Then you suspend the process: ^Z [1]+ Stopped sleep 3000 You can list the process: $ jobs [1]+ Stopped sleep 3000 and bring it back to the foreground: $ fg %1 sleep 3000 The %1 corresponds to the [1] listed ...


7

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 process in the foreground process group. A ...


6

The jobs are not killed, they are suspended. They remain exactly as they are at the time of the suspension: same memory mapping, same open file, same threads, … It's just that the process sits there doing nothing until it's resumed. It's like when you pause a movie. A suspended process behaves exactly like a process that the scheduler stubbornly refuses to ...


6

jobs is not a real command, but a command that is builtin to the shell that you're using: martin@dogmeat:~$ type jobs jobs is a shell builtin When you try to run it without a shell, you'll get an error message, because there is no binary executable called jobs. It also doesn't have a manpage because it's just a builtin. Look in man builtins as Marco ...


5

From a user's perspective, it means that the job is paused. It will no longer use any CPU. It will, however, keep using the same amount of RAM. That is why you can bring it back to the foreground with fg and it will continue where it left off. If you kill a job and then restart it, it will start over from scratch.


5

This is possible in zsh, and in fact it's easy thanks to the direct access to the job parameters provided by the zsh/parameter module. You can use a job number or any job specification (%+, %-, %foo, etc.) as a subscript in the array. zmodload zsh/parameter fgcd () { local dir=$jobdirs[${1:-%+}] # If the jobspec matched, then call cd. Otherwise it's ...


4

When you start a program in a terminal, the terminal will "hang" until your program stops. By pressing Ctrl+c you are closing your program, and thus get back to the prompt. You will see this with all GUI apps, try Firefox, for example. When you use some other method such as Alt+F2 or clicking through the menus, your program is started in the background so ...


4

EDIT: Once in the foreground, you can Ctrl+C, or as @Zelda mentions, kill with the '%x' where 'x' is the job number will send the default signal (most likely SIGTERM in the case of Linux). just type fg to bring it to the foreground, if it was the last process you backgrounded (with '&'). If it was not the last one, type: jobs and find the 'job number', ...


4

After a process is sent to the background with &, its PID can be retrieved from the variable $!. The job IDs can be displayed using the jobs command, the -l switch displays the PID as well. $ sleep 42 & [1] 5260 $ echo $! 5260 $ jobs -l [1] - 5260 running sleep 42 Some kill implementations allow killing by job ID instead of PID. But a ...


3

If your intent is to get the working directory of a process, this is one way: ~ » jobs -l [1] + 14308 running sleep 1h ~ » readlink /proc/14308/cwd /home/matti-nillu The following function inside .bashrc, will do exactly what you want cdjob () { pid=$(jobs -p $1); d=$(readlink /proc/$pid/cwd); cd "$d" } Example: ~$ sleep 1h & [1] ...


3

If the issue is you're finding it difficult to keep track of what you're working on as you bounce from task to task you might want to take the time to look at using a tool such as tmux and/or screen. These are virtual terminal window servers and allow you to setup a terminal within them and name it. This allows you to put some context on a terminal and keep ...


3

You have two options, I think: $BASHPID or $! echo "version: $BASH_VERSION" function abc() # wait for some event to happen, can be terminated by other process { echo "inside a subshell $BASHPID" # This gives you the PID of the current instance of Bash. sleep 3333 } echo "PID: $$" # (i) abc & echo "PID: $$" # (ii) echo "another way ...


3

This is a tad tricky because of quoting, note change from " to ' The following will work if you submit your at job via at -f file at -f nc.on now cat nc.on bash -c 'while [ 1 ]; do echo $$ > /var/run/atnc.pid; nc -l -p 1111 >> check; done' the file /var/run/atnc.pid will have the process id of the bash which is running nc You can cat the file ...


3

Programs run via a shell run in the foreground of that shell by default. This causes the shell to suspend operation and direct stdin/stdout/sterr from the terminal to the program. Programs run via the desktop environment are forked, which causes them to run independent of the program that ran them. This can be simulated in most shells by appending a & to ...


3

I can get that error message with: $ ash -c $'Steve\rjobs\\ ' jobs : not found ash actually outputs: ash: 1: Steve\rjobs : not found But on a terminal, that \r moves the cursor to the beginning of the line, and the jobs: not found overwrites the ash: 1: Steve. Other than that, your error message may suggest that whatever tried to execute the jobs ...


2

The & backgrounds well except for programs that come back requiring console interaction later on (for example, an "apt -y update &" that eventually enters the STOP state since it's wanting to prompt the user a "really really force?" question much later....when no one is watching anymore). To plug that hole and inform the process a terminal will ...


2

The job can be further controlled by sending appropriate signals (using kill command for example). You may try this: run some long running command (yes for example, as from its output we can see, that the process is running) press Ctrl + Z determine process pid: pgrep yes resume process (equivalent to bg or fg) using: kill -CONT <PID>, where ...


2

I would set up a queue directory and have a background process go through the queue directory and spawn the ffmpeg conversion. And example of the background program might be: queue=/var/tmp/vidq outdir=/var/tmp/videos while true; do # do forever for file in $queue/*; do name=`basename $file` mv -f $file $queue/.current ffmpeg ...


2

From man pages: The character '+' identifies the job that would be used as default for the fg or bg utilities; this job can also be specified using the job_id %+ or "%%" . The character '-' identifies the job that would become the default if the current default job were to exit; this job can also be specified using the job_id %-.


2

I usually do something like this: ps aux | grep $PROCESS | grep -v grep | awk '{print $2 " " $11}' For example, if $PROCESS="bash", then this is the output from my current session: 2683 bash 4481 bash 5695 bash Then I would kill $PID for whichever /bin/bash I no longer want to be running. ps, grep and (optionally) awk are helpful for hunting down PIDs ...


2

Your problem is that the way your command is written, sh is trying to handle the herestring redirection, and (being linked to dash, not bash, in Ubuntu) it doesn't have the <<< operator. You probably want this: echo 'bash -c "do-smth.sh &>>logfile"' | at now + 8 hours


2

As you noted, invoking firefox a second time will simply ask the running instance to open another window. The -no-remote switch can be used to inhibit this behavior. Something similar happens with nautilus: it is used to display the desktop window (with it's icons), so it's already running when you start it.


2

If you're running similar commands many times, put the similar parts in a shell script or in a makefile. Give the script or the target meaningful names. That way, when you look at what your various terminals are doing, the command line (plus possibly the host name and the current directory) will tell clearly you all you need to know. Set your terminal's ...


2

It is common to rotate logs periodically, rotating them at midnight is common. Many applications will do this automatically. For those that don't there are tools like logrotate that will do the rotation. Many programs are configured to reopen their logs when sent a HUP signal, and this is one of the techniques used by logrotate. Things to check: Do ...



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