Commands tagged background (15)

  • doesn't require "at", change the "2h" to whatever you want... (deafult unit for sleep is seconds)


    25
    ( ( sleep 2h; your-command your-args ) & )
    sitaram · 2009-08-19 17:39:11 12
  • Very useful in shell scripts because you can run a task nicely in the background using job-control and output progress until it completes. Here's an example of how I use it in backup scripts to run gpg in the background to encrypt an archive file (which I create in this same way). $! is the process ID of the last run command, which is saved here as the variable PI, then sleeper is called with the process id of the gpg task (PI), and sleeper is also specified to output : instead of the default . every 3 seconds instead of the default 1. So a shorter version would be sleeper $!; The wait is also used here, though it may not be needed on your system. echo ">>> ENCRYPTING SQL BACKUP" gpg --output archive.tgz.asc --encrypt archive.tgz 1>/dev/null & PI=$!; sleeper $PI ":" 3; wait $PI && rm archive.tgz &>/dev/null Previously to get around the $! not always being available, I would instead check for the existance of the process ID by checking if the directory /proc/$PID existed, but not everyone uses proc anymore. That version is currently the one at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html but I plan on upgrading to this new version soon. Show Sample Output


    14
    sleeper(){ while `ps -p $1 &>/dev/null`; do echo -n "${2:-.}"; sleep ${3:-1}; done; }; export -f sleeper
    AskApache · 2009-09-21 07:36:25 8
  • I needed a way to search all files in a web directory that contained a certain string, and replace that string with another string. In the example, I am searching for "askapache" and replacing that string with "htaccess". I wanted this to happen as a cron job, and it was important that this happened as fast as possible while at the same time not hogging the CPU since the machine is a server. So this script uses the nice command to run the sh shell with the command, which makes the whole thing run with priority 19, meaning it won't hog CPU processing. And the -P5 option to the xargs command means it will run 5 separate grep and sed processes simultaneously, so this is much much faster than running a single grep or sed. You may want to do -P0 which is unlimited if you aren't worried about too many processes or if you don't have to deal with process killers in the bg. Also, the -m1 command to grep means stop grepping this file for matches after the first match, which also saves time. Show Sample Output


    10
    sh -c 'S=askapache R=htaccess; find . -mount -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e "s%${S}%${R}%g" FF'
    AskApache · 2009-10-02 05:03:10 7
  • This command runs your shell script in the background with no output of any kind, and it will remain running even after you logout.


    7
    nohup /bin/sh myscript.sh 1>&2 &>/dev/null 1>&2 &>/dev/null&
    AskApache · 2009-08-18 07:24:52 14
  • This is helpful for shell scripts, I use it in my custom php install script to schedule to delete the build files in 3 hours, as the php install script is completely automated and is made to run slow. Does require at, which some environments without crontab still do have. You can add as many commands to the at you want. Here's how I delete them in case the script gets killed. (trapped) atq |awk '{print $1}'|xargs -iJ atrm J &>/dev/null


    1
    echo "nohup command rm -rf /phpsessions 1>&2 &>/dev/null 1>&2 &>/dev/null&" | at now + 3 hours 1>&2 &>/dev/null
    AskApache · 2009-08-18 07:31:17 9
  • Need package: gksu Note: Launching gui app in background that needs sudo, won't work great with our old friendly style of launching: sudo gedit /etc/passwd & because this would put sudo in background ! Using gksudo as demonstrated, would popup a gui sudo window. May be this is a common knowledge, but not knowing this frustrated me during my newbie year.


    1
    gksudo gedit /etc/passwd &
    b_t · 2010-10-05 13:11:04 7

  • 1
    /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &
    adkatrit · 2011-01-14 18:53:11 5
  • You're running a program that reads LOTS of files and takes a long time. But it doesn't tell you about its progress. First, run a command in the background, e.g. find /usr/share/doc -type f -exec cat {} + > output_file.txt Then run the watch command. "watch -d" highlights the changes as they happen In bash: $! is the process id (pid) of the last command run in the background. You can change this to $(pidof my_command) to watch something in particular. Show Sample Output


    1
    watch -d "ls -l /proc/$!/fd"
    flatcap · 2014-01-31 23:51:17 8
  • Simple way of having random mrxvt backgrounds. Add this to your bashrc and change the path names for the pictures.


    0
    LIST="/some/pic/file /another/picture /one/more/pic"; PIC=$(echo $LIST | sed s/"\ "/"\n"/g | shuf | head -1 | sed s/'\/'/'\\\/'/g ); sed -i s/Mrxvt.Pixmap:.*/"Mrxvt.Pixmap:\t$PIC"/ ~/.mrxvtrc
    dog · 2010-08-23 10:17:42 3
  • "The -b (background) option tells sudo to run the given command in the background." -- after it asks you for the password in the foreground. Show Sample Output


    0
    sudo -b xterm
    shavenwarthog · 2010-10-05 23:03:01 4
  • List background jobs, grep their number - not process id - and then kill them Show Sample Output


    0
    jobs | grep -o "[0-9]" | while read j; do kill %$j; done
    haggen · 2012-04-12 17:29:58 6
  • Run a nohup script in background launched trough a shell script without interrupting the main shell script execution.


    0
    nohup some_command/script.sh > /dev/null 2>&1&
    klausro · 2016-10-21 07:17:05 16

  • 0
    (nohup your-command your-args &>/dev/null &)
    socketz · 2018-05-23 17:35:43 188
  • Lower PowerShell priority, so that to launch processes in the background and work normally with other applications Show Sample Output


    0
    PS> Get-WmiObject Win32_process -filter 'name = "powershell.exe"' | Foreach-Object { $_.SetPriority(16384) }
    pascalvaucheret · 2022-06-16 14:53:28 389
  • Take advantage of sudo keeping you authenticated for ~15 minutes. The command is a little longer, but it does not require X (it can run on a headless server).


    -3
    sudo ls ; sudo gedit /etc/passwd &
    aporter · 2010-10-05 21:01:34 5

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Show complete URL in netstat output
The -W switch of netstat makes it print complete URL of the connections, which otherwise by default is truncated to fit its default column size. Now to compensate for irregular column sizes, pipe the output to column (-t switch of column prints in tabular form). The only downside to this part is that the very first row, the header, goes pear shape.

Show one line summaries of all DEB packages installed on Ubuntu based on pattern search
I sometimes want to know what packages are installed on my Ubuntu system. I still haven't figured out how to use aptitude effectively, so this is the next best thing. This allows finding by name. The grep '^ii' limits the display to only installed packages. If this is not specified, then it includes listing of non-installed packages as well.

swap stdout and stderr
Possible use, to filter something in stderr: (/usr/bin/$COMMAND $PARAM 3>&1 1>&2 2>&3 | grep -v $uninteresting_error ) 3>&1 1>&2 2>&3

Display the definition of a shell function
Display the code of a previously defined shell function.

Remove a range of lines from a file

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Benchmark SQL Query
Benchmark a SQL query against MySQL Server. The example runs the query 10 times, and you get the average runtime in the output. To ensure that the query does not get cached, use `RESET QUERY CACHE;` on top in the query file.

Rotate a set of photos matching their EXIF data.
You need jhead package.

raw MySQL output to use in pipes
-N removes header -s removes separator chars -r raw output After using these options, the MySQL ouptut can be used with pipes very easily

Rotate a video file by 90 degrees CW


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: