Launching programs in background and async
&
- launches program in background in async, use
fg command to bring back to foreground
`{command} &
nohup
- prevents SIGHUP to reach the program and write stdout to
nohup.out file
nohup {command}
disown (bash, ksh, zsh)
- detach the process from job queue of user, it makes
init as the parent of the process
Redirection
- Overwrite
{command} < {file} : sends input to stdin from file
{command} > {file} : overwrite output of stdout to file
- Append
{command} << {delimiter} : (here-document) reads input until it gets delimiter which marks ending of input
- Syntax
user:~$ {command} << {delimiter}
here-document
delimiter
- Example
user:~$ cat << stop
> Hi World
> How are you
> stop
Hi World
How are you
user:~$
{command} >> {file} : append output of stdout to file
- Merge
p >& q : Merges output from stream p with stream q
p <& q : Merges input from stream p with stream q
{command1} | {command2} : (pipe) sends stdout of one command to stdin of another command
- Whenever you execute a program/command at the terminal, 3 files are always open
viz., standard input, standard output, standard error
file descriptors (fd) and redirection:
# reads stdin data from infile
{command} 0<infile
# writes stdout to outfile
{command} 1>outfile
# appends stderr to logfile
# >> is append, > is overwrite
{command} 2>>logfile
redirecting stderr (2) to stdout (1)
{command} 2>&1
write stdout nowhere
{command} > /dev/null
{command} 1>/dev/null
write stdout and stderr nowhere
{command} > /dev/null 2>&1
write stdout and stderr to file
{command} > file 2>&1