Everyone knows and loves screen for running longtime scripts in the background without worrying that the ssh connection will drop and will have to run it again. Still, I have found myself many times in the situation where I started a process and needed to put it in the background and run something else on the console. Uff.. If only I started it with screen. But wait, there is hope. This quick tip will show how to put a process in the background and then start it back in foreground.
This works in bash and uses the ‘suspend‘ key (CTRL+Z) and the bg – background and fg – foreground commands. Let’s say we were running an intensive rsync command, and are wanted to check if we still have the available space on the disk without opening a new ssh session (yes, I know):
rsync -ar server:/source/ /destination/
^Z
Stopped
Let it run in the background:
bg
[1] rsync -ar server:/source/ /destination/ &
Now we can run some other commands like du:
du -h
We can see the background process with ps or jobs:
jobs
[1] Running rsync -ar server:/source/ /destination/
And finally we can bring it back to foreground with fg:
fg
Note: this works only on the running ssh/bash session and it will be closed once you exit. Logout should warn about open/running jobs and that they will be lost if exit.
Tags: bash, screen, tips, Tips & Tricks
I needed to get a list of all the subdirectories that were owner by some other user than root under /var and their permissions/owner with full paths. My first thought was to use ls and something like this:
ls -dlR */
drwxr-xr-x 2 root root 4096 2009-06-05 06:25 backups/
drwxr-xr-x 8 root root 4096 2009-05-11 06:02 cache/
drwxr-xr-x 2 root root 4096 2009-05-06 04:49 ec2/
drwxr-xr-x 25 root root 4096 2009-05-25 14:55 lib/
...
will show the subdirectories just as I needed but only at one level. Using */*/ would show the next level, etc. This obviously is not a solution and unfortunately I had found no other way to do this with ls. Using:
ls -alR | grep ^d
drwxr-xr-x 15 root root 4096 2009-05-11 06:02 .
drwxr-xr-x 22 root root 4096 2009-06-03 15:02 ..
drwxr-xr-x 2 root root 4096 2009-06-05 06:25 backups
drwxr-xr-x 8 root root 4096 2009-05-11 06:02 cache
drwxr-xr-x 2 root root 4096 2009-05-06 04:49 ec2
drwxr-xr-x 25 root root 4096 2009-05-25 14:55 lib
....
works somehow, but since I don’t have the full paths this is useless.
Read the rest of this entry »
Tags: tips, Tips & Tricks
Here is a quick bash tip that might be useful if you need to use inside a bash script a check to see if a wildcard expression of files/folders exists or not. For example:
if [ -e /tmp/*.cache ]
then
echo "Cache files exist: do something with them"
else
echo "No cache files..."
fi
This is using -e (existing file check) that is working fine on individual files. Also the above code might seem to work fine if the result of the expression if one file; but if you have more files returned by the expression this will fail with the following error:
line x: [: too many arguments
Read the rest of this entry »
Tags: bash, tips, Tips & Tricks
There are various methods to run multiple instances of mysql (on different ports) on the same machine. We can either compile the mysql binary with different defaults and paths, use mysqld_multi or the MySQL Sandbox project. Still the simplest solution I’ve used in the past for such situations is to use the same binary and use a separate configuration file (with separate port, pid, socket and data directory). This post will explain this method. Read the rest of this entry »
Tags: mysql, tips, Tips & Tricks
The % character is a special char for crontab entries, and in order to use it we have to escape it. For example a crontab entry like this:
1 0 * * * /bin/sleep `/usr/bin/expr $RANDOM % 600` ; /usr/local/bin/mycommand
that attempts to run mycommand hourly with a random delay for the start, will fail because cron will see the % character and ignore the rest of the command; it will just run: /bin/sleep `/usr/bin/expr $RANDOM that will return a random number between 0-32767 and exit.
Read the rest of this entry »
Tags: cron, tips, Tips & Tricks