Automatically Start a Script at Linux Bootup

2010 January 27
by SendDerek

Say you want to automatically start programs when starting up Linux. A quick and easy way to make a script or program start when Linux does is through the init scripts located in the /etc/rc2.d/ directory. These scripts are called in order of priority when Linux enters runlevel 2. Your default runlevel may be different than 2, so be sure to check with the ‘runlevel’ command or in the /etc/inittab. In order for the scripts here to start, they must follow a three-part naming schema. For example: /etc/rc2.d/S10thisscript. This script will Start with priority of 10 when the system enters runlevel 2 and is described as thisscript. The description doesn’t matter, but it is useful to give it a meaningful name.  For more information, please see this page.

Here’s a very quick and dirty walkthrough that will change the IP address whenever the system is booted:

echo "ifconfig eth0 192.168.1.102" > /etc/init.d/changeip
chmod +x /etc/init.d/changeip
ln -s /etc/init.d/changeip /etc/rc2.d/S10changeip

So, you can see that we create a script called changeip in the /etc/init.d/ directory, gave it executable permissions, and then created a symbolic link to /etc/rc2.d/S10changeip so that it will run when the system is started.  As a general note, the script can be located anywhere as long as the symbolic link is targeted at /etc/rc2.d/S10changeip.

If you are going to be calling a binary that needs to run in the background, you’ll want to use the nohup and sleep commands like below to avoid having your application be terminated with the init process (the sleep command will prevent the nohup command from being killed before it’s initialized — ironic):

   nohup myprog &
   sleep 1

Along the same lines as the above trick, a script can be made to run whenever a user logs in by placing the name and location of your script into the ~/.profile directory.  For example:

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
 if [ -f ~/.bashrc ]; then
 . ~/.bashrc
 fi
fi

mesg n

echo "Now running user script..."
/home/username/changeip

Happy scripting!
Note: I’ve just learned that on Debian systems, all you may need to do is create the script, make it executable, and then call the “update-rc.d” command like such:

chmod +x the_script_name
update-rc.d the_script_name defaults

Source: http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/

Statically Assign /dev Nodes to Hardware Devices in Linux

2010 January 26
by SendDerek

A handy trick for always making sure that a particular device is assigned a particular node in /dev is to use local udev rules.  This way, you’ll always know that the device you plug in will always be at the same spot (ie. your USB thumb drive will always mount to /dev/thumb instead of /dev/sdxx).  Some folks may refer to this as static assignment or forcing a /dev node.  I took advantage of udev local.rules so that my 160GB external HDD with media will always mount to /dev/media and my 500GB external HDD with video will always mount to /dev/video.  This way, I can edit my fstab rules to always mount /dev/media to /mnt/media and /dev/video to /mnt/video and I don’t have to guess which HDD belongs to /dev/sdb or /dev/sdc.  Here are the steps I took:

1.) Plug in your device and find out which /dev node it was assigned by using “dmesg” output or “fdisk -l”. Then, get a unique piece of information about it. As long as it is unique, it should work. In my case, each HDD has a different model number, so I used the ATTRS{vendor} attribute as a unique identifier. Use the following command to view device information:

 udevinfo -a -p $(udevinfo -q path -n /dev/sdb)

2.) You’ll need to create a local.rules file and fill it with the unique information from the above command (see my example below).

touch /etc/udev/rules.d/10-local.rules

My example:
This tells udev to create a symlink to /dev/video when a device with a vendor attribute of ST350032 is detected and the same applies for line two.

ATTRS{vendor}=="ST350032", SYMLINK+="video"
ATTRS{vendor}=="ST316002", SYMLINK+="media"

3.) Use the following command to restart udev and then unplug and re-plugin the device to verify that you indeed have a new /dev/mydevice node.

/etc/init.d/udev restart

You can then further take advantage of this by editing your /etc/fstab file to automatically mount the drive. Like mine for example:

/dev/video     /mnt/video     ntfs-3g     defaults,locale=en_US.utf8   0    0
/dev/media     /mnt/media     ntfs-3g     defaults,locale=en_US.utf8   0    0

Additional Information:
http://reactivated.net/writing_udev_rules.html

Additional Guides:
http://ubuntu-tutorials.com

Writing Images to Disk on Mac OSX with dd

2010 January 25
by SendDerek

1.) Become root:

su

2.) Plug in your SD card, HDD, or other block device and then use the following command to see which /dev/diskN node it’s located on:

diskutil list

3.) Unmount the disk where “N” is the number of the disk taken from the above command:

diskutil unmountDisk /dev/diskN

If the above command was successful, you will see:

Unmount of all volumes on diskN was successful

4.) Use the ‘dd’ command to copy the image file (.dd or .img generally) to the entire disk:

dd if=myImage.dd of=/dev/diskN

Also, you can write the image to particular partitions of the disk with (N is the disk number and P is the partition number):

dd if=myPartitionImage.dd of=/dev/diskNsP

The process to do this under Linux is very similar except that it’s not required to un-mount the drive before using the ‘dd’ command and the commands are a little different. For example, you would use “fdisk -l” instead of “diskutil list”, your device node would be located at “/dev/sda” instead of “/dev/disk” and the un-mount command is “umount” instead of “diskutil unmountDisk”.

Set Serial IRQ, Baud Rate, and More with ‘stty’ and/or ‘setserial’

2010 January 22
by SendDerek

The stty and setserial programs are useful for setting up serial ports on a Linux box. For example, today I needed to set the IRQ and baud rate of two serial ports in order to get them to play nicely. So, I used setserial to setup the IRQ and then stty to setup the baud rate, like this:

root@ts7000:root# setserial /dev/tts/2
/dev/tts/2, UART: 16550A, Port: 0x89c003e8, IRQ: 33
root@ts7000:root# setserial /dev/tts/3
/dev/tts/3, UART: 16550A, Port: 0x89c002e8, IRQ: 40
root@ts7000:root# stty -F /dev/tts/2 115200
root@ts7000:root# stty -F /dev/tts/3 115200

Now that I look at it, I could have set the baudrate with setserial /dev/tts/2 baud_base 115200, but then I wouldn’t have been introduced to the good little program stty!

Of course, I’d encourage you to take a look at the man pages that I linked to in the first sentence so you can learn all about the neat features of both programs.

Quickly Search the Terminal History in Unix Using Ctrl+R

2010 January 21
by SendDerek

A neat little trick for getting around the terminal history is by using the “reverse-i-search” feature of the terminal. To use this, simply hit [ctrl+r] in the terminal. What you’ll see is

(reverse-i-search)`':

Simply start typing and it will find previously used commands. You can hit [ctrl+r] again to search through the history if there is more than one match. For example, I typed in “hi” and it found the text pattern in my history of the command “which ifconfig”. At this point, I can hit [enter] and it will re-run the command or I can modify the command if I choose with the arrow keys like normal.

(reverse-i-search)`hi': which ifconfig

Happy terminaling!