doesn't require "at", change the "2h" to whatever you want... (deafult unit for sleep is seconds)
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
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
This command runs your shell script in the background with no output of any kind, and it will remain running even after you logout.
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
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.
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
Simple way of having random mrxvt backgrounds. Add this to your bashrc and change the path names for the pictures.
"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
List background jobs, grep their number - not process id - and then kill them Show Sample Output
Run a nohup script in background launched trough a shell script without interrupting the main shell script execution.
Lower PowerShell priority, so that to launch processes in the background and work normally with other applications Show Sample Output
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).
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.
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
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: