I have a small bash utility script that I use to run my commands in the background. For instance, I do a
git push in the background
b git push
This utility runs the command in the background and notifies me when it is complete. It also tells me if the command succeeded or failed. Here it is:
#!/bin/bash #Script to run a command in background redirecting the #STDERR and STDOUT to /tmp/b.log in a background task echo "$(date +%Y-%m-%d:%H:%M:%S): started running $*" >> /tmp/b.log cmd="$*" (/bin/bash -l -c "$cmd" 1>> /tmp/b.log 2>&1; notify-send --urgency=low -i "$([ $? = 0 ] && echo '/home/goodcode/.icons/ruby_green_icon.png' || echo error)" "$cmd")&>/dev/null &
Let us split this command and understand what it does
/bin/bashinvokes the bash command, this is useful when you want to execute arbitrary commands using bash after constructing them in a script.
-luses the login option
-cto pass the actual command
2>&1to redirect the standard error stream to the standard out stream
1>> /tmp/b.logto redirect the standard out (which also has the standard error because of the above) to a file called
/tmp/b.login append mode.
;separator between commands to run the next command regardless of the success of the first command
notify-sendto show a notification on our screen, this is executed once the first command is finished
$([ $? = 0 ] && echo '/home/goodcode/.icons/ruby_green_icon.png' || echo error):
$?gives us the exit value of the previous command and if it is equal to
0it means the previous command was successful, and in this case it uses a green ruby icon, otherwise it uses a red error icon.
&> /dev/nullredirects the stdout and stderr of this entire command to
/dev/nullwhich is a null device, which discards all that is written to it. So, basically it is as if we are writing in to the void
&at the end backgrounds the command