Hot answers tagged jobs
20
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 &
...
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 ...
6
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 ...
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 ...
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
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.
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 ...
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 1; do # do forever
for file in $queue/*; do
name=`basename $file`
mv -f $file $queue/.current
ffmpeg -i ...
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
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
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 ...
2
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 ...
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.
1
The single quotes made bash not substitute arguments. As manatwork mentioned, double quotes are interpreted less strictly.
manatwork wrote:
awk "NR==$1{print \$$2}"
Still, the "echo `...`" part in my function posed a problem, but it seems I overcomplicated things and it isn't needed at all. This new function works for me:
function jf {
jobs | awk ...
1
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 %-.
1
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 ...
1
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 immediately. If you press Ctrl+C then ...
1
You can have more than one job running or paused in the background:
$ ./reallybigjob &
[1] 873
$ ./otherbigjob &
[2] 875
$ jobs
[1]- Running ./reallybigjob &
[2]+ Running ./otherbigjob &
$ fg 1
./reallybigjob
^Z
[1]+ Stopped ./reallybigjob
$ fg 2
./otherbigjob
^Z
[2]+ Stopped ...
1
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 ...
Only top voted, non community-wiki answers of a minimum length are eligible