3 Jun, 2008  |  Posted by Danesh  |  in HowTo, Linux

Sending a process to the background in Linux is quite easy. All you need is bg, fg, &, and ctrl+Z ( ^Z ).

For this example I will use a simple bash script test.sh I put together to print “Test” every 5 seconds.

#!/bin/bash
#This script will print "Test" every 5 seconds
#
while [ true ]
do
echo “Test at `date`”
sleep 5
done
#End

Now let’s see how it’s done.

[user@abubu root]$./test.sh &
This starts test.sh and sends it to the background. You will be back at shell but should see the “Test” message every 5 seconds.

[user@abubu root]$jobs
[1]+ Running ./test.sh &

The jobs command will print all the background processes. Each process is represented by a number to it’s left. For example, tesh.sh is represented by 1.

[user@abubu root]$fg 1
The fg command will send the test.sh process to the foreground and return control to the shell.

[user@abubu root]$ ./test.sh (hit ctrl+Z (^Z) now)
Test at Tue Jun 3 15:11:38 MYT 2008
[1]+ Stopped ./test.sh

The test.sh process is temporarily suspended.

[user@abubu root]$bg 1
The bg command will send test.sh to the background.

[user@abubu root]jobs
[1]+ Running ./test.sh &

The jobs command will print all the background processes. Each process will be represented by a number to it’s left. tesh.sh is represented by 1.

[user@abubu root]$fg 1
The fg command will send the test.sh process to the foreground and return control to the shell.

That’s it.

21 Jan, 2008  |  Posted by Danesh  |  in HowTo, Linux

I develop quite a few bash scripts to automated backups and dropbox operation in my office. One of the usual requirements is to tar (tar.gz) the files locally and later untar them on the destination server when needed. I have a few simple scripts with interactive menus which help the data center operations team with their daily backup tasks.

One of the challenges during development was the ability to untar files on the backup server over SSH. The command by default will untar to the home directory instead of the target folder output always returned success but the files were nowhere to be found. This apparently is a limitation on the tar command, it did not know where to untar the files to when being executed over SSH. Fortunately the fix was really simple.

Original command,

ssh 127.0.0.1 'tar zxvf ~/test/files.tzr.gz'

The simple fix,

ssh 127.0.0.1 'cd ~/test/ ; tar zxvf files.tar.gz'

or, (thanks Aik)

ssh 127.0.0.1 'tar zxvf files.tar.gz -C ~/test/'

Here’s a video I put together to demonstrate the above. Hopefully I got it right.

Continue Reading ->

16 Jan, 2008  |  Posted by Danesh  |  in HowTo, Linux

Need to find files older than certain time frame? This will help, “find [dir] -type f -mtime +[24hours*n] ”

Examples,

Show files older than 7 days

find /tmp/ -type f -mtime +7

Show files older than 7 days and rm them.

find /tmp/ -type f -mtime +7 -exec rm {} \;

or if you have a large number of files

find /tmp/ -type f -mtime +7 | xargs rm
10 Jan, 2008  |  Posted by Danesh  |  in Linux

My friend wanted to know how to select all files but one on the CLI or in a bash script. This is how I normally do it, do you know a better way?

From the command line

ls * | grep -v [pattern to ignore]

or

ls [!pattern to ignore]  *

in a bash script it may look like this,


for i in `ls * | grep -v [pattern to ignore]`
do
   do something here
done

Continue Reading ->