Archive for the ‘script’ Category

Start up Sound

15, March 2011

Start up Sound:

This works on my Xubuntu Natty install. I found a .ogg sound file that I wanted to use as a start up sound so needed an command line ogg player to play it.

$ sudo apt-get install ogg123

Now create a script in /usr/bin as follows [replace mousepad with the text editor of your choice];

$ sudo mousepad /usr/bin/startup

Add the following to the file

#!/bin/sh
#### play sound at startup ####
## reduce volume ##
sleep 5
StartupNotify=false
/usr/bin/amixer -c 0 sset Master,0 60%
## play sound ##
/usr/bin/ogg123 /path_to_my/sartupsound.ogg
## increase volume again ##
/usr/bin/amixer -c 0 sset Master,0 85%

save the script and make it executable with.

$ sudo chmod +x /usr/bin/startup

Now add the script to the system start up, in Xubuntu open [Settings], [Settings Manager], [Session and Startup], [Application Autostart], and then the [Add] button. Add a name of your choice in the Name field, and the same in the Description field, then the path to your script in the Command field i.e /usr/bin/startup in this case the [ok] to finish. You will then get a startup sound next time you log on.

Shutdown Sound

15, March 2011

Shutdown Sound:

This works on my Xubuntu Natty install. I found a .ogg sound file that I wanted to use as a shutdown sound so needed an command line ogg player to play it.

$ sudo apt-get install ogg123

now add a script as follow script to /etc/init.d as follows

In a Terminal, create the file with, [change mousepad to your favourite text editor]

$ sudo mousepad /etc/init.d/K99shutdownsound.sh

copy the following into the script

#!/bin/sh
#### play sound at startup ####
## reduce volume ##
amixer -c 0 sset Master,0 50%
## play sound ##
/usr/bin/ogg123 /path_to my/shutdown.ogg

save the script and make it executable.

$ sudo chmod +x /etc/init.d/K99shutdownsound.sh

now add a shortcut to /etc/rc0.d where shutdown scripts go and to /etc/rc6.d where reboot scripts go

$ sudo ln -s /etc/init.d/K99shutdownsound.sh /etc/rc0.d/K99shutdownsound.sh

$ sudo ln -s /etc/init.d/K99shutdownsound.sh /etc/rc6.d/K99shutdownsound.sh

Now I get a reboot and shutdown sound.

gconftool-2

1, December 2010

Using the gconf tool to make changes to your desktop from the command line. These allow you to script changes you make every time to upgrade.

These have been tested on the Gnome Desktop on Lynx & Maverick.

Copy and paste these commands into a Terminal [Applications], [Accessories], [Terminal]

## Add icons to the [System] menu. ##
gconftool-2 --type Boolean --set /desktop/gnome/interface/menus_have_icons True

## Remove shutdown “Do you really want to do this 60 second count down” question ##
gconftool-2 --type Boolean --set /apps/indicator-session/suppress_logout_restart_shutdown True

## Return to old Update notification icon in top menu ##
gconftool-2 --type Boolean --set /apps/update-notifier/auto_launch False

gconftool-2 --type int --set /apps/update-notifier/regular_auto_launch_interval 0

## Add Banner message to GDM ##
sudo -u gdm gconftool-2 --set --type string /apps/gdm/simple-greeter/banner_message_text “Ubuntu 10.10 Maverick Meerkat 64 Bit”

sudo -u gdm gconftool-2 --set --type string /apps/gdm/simple-greeter/banner_message_text_nochooser “Ubuntu 10.10 Maverick Meerkat 64 Bit”

sudo -u gdm gconftool-2 --set --type boolean /apps/gdm/simple-greeter/banner_message_enable true

## Set wallpaper ##
gconftool-2 --type string --set /desktop/gnome/background/picture_filename /home/Pictures/My_Wallpaper.jpg

Sheevaplug – automount USB drive at boot

17, February 2010

The problem with the Sheevaplug is that it boots too damn fast and the USB drive is too slow to be available for the fstab mount.

The solution, see here for my post on setting up the fstab file to mount your USB drive. When you edit the fstab on the Sheeva you will find it empty but you should end up with an entry similar to this.

# UNCONFIGURED FSTAB FOR BASE SYSTEM
/dev/sda1 /mnt/usbdrive ext3 auto,user,rw,exec 0 0

My usb drive is mounted at /dev/sda1 and I created a mount point called /mnt/usbdrive, the drive is formatted as ext3 but you can replace this with the format of your drive or just replace with the word auto for it to check.

Once you have your fstab in place you need to create a script which will slow down the boot for the USB drive to start. Thanks to restamp on the plugcomputer.org forum see here for the solution.
First navigate to the init.d folder, in a [Terminal] issue the command;

cd /etc/init.d

then using whatever text editor you have installed [I added nano], open a file called wait4usbdrive with;

nano wait4usbdrive

then copy into it restarts script;

#!/bin/sh
#
# If /etc/fstab has been configured to mount a USB drive, pause to give
# the USB drive devices time to show up in /dev. If this is not done,
# checkfs.sh will fail, requiring manual intervention…
#
case "$1" in
start)
grep -q ^/dev/sda /etc/fstab &&
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do
[ -b /dev/sda ] && exit 0
sleep 1
done
exit 1
;;
stop)
;;
esac

Then make it executable with;

chmod +x wait4usbdrive

Then link it to the rcS.d folder with this command;

ln -s ../init.d/wait4usbdrive /etc/rcS.d/S25wait4usbdrive

Now you can shutdown and restart the plug and the USB drive should now auto-mount. My USB drive is a 1.5 TB iomega with it’s own power supply.

Quake One

13, December 2009

Well it’s been a while since I’ve played this but I finally got it working in Ubuntu Karmic using glquake, the .pak files from my Quake One CD and a script using the info. from this thread at http://forum.eeeuser.com I created a folder called quake_one and within it another called id1, I then copied the .pak files from the CD to the id1 folder making sure their filenames were in lowercase. Then copied the glquake file to the quake_one folder. Now I created a script in my path called quake_one.sh with the following in it;

#!/bin/sh
## Update Permissions
sudo chmod 666 /proc/asound/card0/pcm0p/oss
sudo chmod 666 /proc/asound/card0/pcm0c/oss
echo “Permissions changed…”
## change to quake one directory ##
cd ~/quake_one
## redirect quake sound ##
sudo echo “glquake 0 0 direct” > /proc/asound/card0/pcm0p/oss
sudo echo “glquake 0 0 disable” > /proc/asound/card0/pcm0c/oss
## run quake one prog ##
./glquake

saved it to ~/bin  then made the script executable with;

chmod +x ~/bin/quake_one.sh

Now run quake_one.sh in a [Terminal] or create a launcher pointing to this script, all done.

Backup using rsync, SSH & cron

31, August 2009

Backup on Ubuntu.

Updated 09/11/09 to correct some errors!

This “How To” was written with the help of this article by Troy Johnson, here and amended were necessary to fit my needs.

I have 2 PC’s on a network and want to backup some files from one PC the [remote_client] to the other PC the [server] to do this I want to run a script from the server. The process can be achieved using rsync & SSH and automated with a cron job on the server.

Step:1

We install on the server

open-ssh-server and rsync

and on the client openssh-client and rsync

This can be done via the Synaptic Package Manager or using the command line i.e.

~$ sudo apt-get install openssh-client

~$ sudo apt-get install rsync

you also may want to check that cron is installed and if you want a GTK version of rsync you could install gadmin-rsync.

So the files I want to backup are on the remote_client in the following folder; /home/martin/my_files

and I want to back them up on the server in the following folder; /home/martin/backups

Note: I have an account on both PC’s with the same name and password, and read access to both the source & destination folders.

On the server we test out a basic script called backup.sh and saved with the Permissions set to “allow executing as a program”, check out my post here for help with that, the content of the script is;

#!/bin/sh

## first script to copy files from remote_client to server ##

rsync -avz -e ssh martin@remote_client:/home/martin/my_files/ /home/martin/backups

save the script and then run it by typing;

~$ ./backup.sh

in a Terminal assuming you saved the script in your home directory /home/martin and called it backup.sh. You should be prompted for your user password on the remote_client then the process should start.

Step:2

Ok so that does the trick but if I’m to automate this script it needs to work without SSH asking me for my password each time. To do this I need to generate a private/public key pair, this is much more secure than adding the actual password in the text of the script or elsewhere linked to it.

We now need to be logged onto the server;

To generate the key pair I open a [Terminal] and issue the command;

~$ mkdir /home/martin/.key

to create a hidden folder in my home folder called .key [the dot makes it hidden], then;

~$ ssh-keygen -t dsa -b 1024 -f /home/martin/.key/server-rsync-key

Generating public/private dsa key pair.

Enter passphrase (empty for no passphrase): [press Enter]

Enter same passphrase again: [press Enter]

Your identification has been saved in /home/martin/.key/server-rsync-key.

Your public key has been saved in /home/martin/.key/server-rsync-key.pub.

The key fingerprint is:

etc. etc.

I now have 2 files in the .key folder in my home folder called;

server-rsync-key

and

server-rsync-key.pub

As you will later set-up a cron job which will run a script that needs access to the junk-rsync-key file we need to change the default ubuntu file permissions to owner, read/write group & other to none

$ chmod 600 server-rsync-key

Now copy the other key, server-rsync-key.pub over to the remote_client into my home folder /home/martin and log onto the remote_client.

Now on the remote_client, I issue the following command in a [Terminal]

~$ if [ ! -d .ssh ]; then mkdir .ssh ; chmod 700 .ssh ; fi

[this checks to see if the folder .ssh exists and if not creates it]

~$ mv server-rsync-key.pub .ssh/

[this moves the key from my home folder to the .ssh folder, hidden in my home folder]

~$ cd .ssh/

[we now move to the .ssh folder]

~$ if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600 authorized_keys ; fi

[this checks to see if there is an authorized_keys file and if not creates it with the correct permissions]

~$ cat server-rsync-key.pub >> authorized_keys

[this copies the contents of server-rsync-key.pub key into the file authorized_keys file.

If we now run our original script with the amendment below on the server it no longer asks for a password.

rsync -avz -e “ssh -i /home/martin/.key/server-rsync-key” martin@remote_client:/home/martin/my_files/ /home/martin/backups

Step:3

Now the key can be used to make connections to the remote_client, but these connections can be from anywhere (that the ssh daemon on remote_client allows connections from) and they can do anything (that remote_user can do), which could be dangerous.  To make this more secure edit the ‘authorized_keys’ file in remote_client /home/martin/.ssh folder with a text editor and modify the line ending user_name@server_name from this:

ssh-dss
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

to this:

from="10.1.1.1", command="/home/martin/.key/validate-rsync" ssh-dss bA402VuCsOLg
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

where “10.1.1.1” is the IP address of server, and “/home/martin/.key/validate-rsync” point to a script called validate-rsync as follows:

#!/bin/sh
case “$SSH_ORIGINAL_COMMAND” in
*\&*)
echo “Rejected”
;;
*\(*)
echo “Rejected”
;;
*\{*)
echo “Rejected”
;;
*\;*)
echo “Rejected”
;;
*\<*)
echo “Rejected”
;;
*\`*)
echo “Rejected”
;;
*\|*)
echo “Rejected”
;;
rsync\ –server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo “Rejected”
;;
esac

If server has a variable address, or shares its address (via NAT etc.) with hosts you do not trust, omit the ‘from=”10.1.1.1″,’ part of the line (including the comma), but leave the ‘command’ portion. This way, only the ‘rsync’ will be possible from connections using this key. Make certain that the ‘validate-rsync’ script is executable by remote_user on remote_client and test it.

So in my case the result looks like this:

command="/home/martin/.key/validate-rsync" ssh-dss bA402VuCsOLg9YS0NKxugT+o4UuIj
yl6b2/cMmBVWO39lWAjcsKK/zEdJbrOdt/sKsxIK1/ZIvtl92DLlMhci5c4tBjCODey4yjLhApjWgvX9
D5OPp89qhah4zu509uNX7uH58Zw/+m6ZOLHN28mV5KLUl7FTL2KZ583KrcWkUA0Id4ptUa9CAkcqn/gW
kHMptgVwaZKlqZ+QtEa0V2IwUDWS097p3SlLvozw46+ucWxwTJttCHLzUmNN7w1cIv0w/OHh5IGh+wWj
V9pbO0VT3/r2jxkzqksKOYAb5CYzSNRyEwp+NIKrY+aJz7myu4Unn9de4cYsuXoAB6FQ5I8AAAEBAJSm
DndXJCm7G66qdu3ElsLT0Jlz/es9F27r+xrg5pZ5GjfBCRvHNo2DF4YW9MKdUQiv+ILMY8OISduTeu32
nyA7 etc....

Our backup.sh script now looks like this:

#!/bin/sh

## second script to copy files from remote_client to server ##

rsync -avz -e “ssh -i /home/martin/.key/server-rsync-key” martin@remote_client:/home/martin/my_files/ /home/martin/backups

Step:4

I now need to run the script from a Cron job, first move the backup.sh script into your $PATH, for help with this check out my post here.

Now on the server open a [Terminal] and issue the command:

~$ crontab -e

This should open a text editor, normally vi, normally paste into it the following text as a reminder

# leave a space between each option #
# the * is a wildcard option #
# dayofmonth is 1 to 31 #
# dayofweek can be 0 – 6, 0 is Sunday #
# or use mon tue wed etc #
#
# mins hours dayofmonth month dayofweek command
# * * * * * * command

Then use Crontrol+o to save and press [Enter] to save, then Control+x to [Exit].

and test…

Cron jobs

31, August 2009

Cron tabs can be used to automate jobs on your system by running programs or scripts at prdefined times, dates or days, each user can have their own cron tabs or they can be run as root. To open a crontab, type the following in a [Terminal];

crontab -e

this will open in the vi text editor, but if you prefere the gedit text editor, run the following command;

export VISUAL=gedit

If you stick with vi then use Control+o followed by [Enter] to save your tab, then Control+x to [Exit].

When you run crontab -e, you will get an empty crontab with just the following [or similar] dispalyed;

# m h dom mon dow command

the headings are described below and I usually add these to the top of the file to make things easier to edit. So my crontab looks like this:

# m h dom mon dow command

# mins -> minutes (0-59)

# hours -> hour (0-24)

# DayOfMonth -> day of month (1-31)

# month -> month (1-12)

# DayOfWeek -> day of week (0-6) [Sunday is 0], or

# DayOfWeek -> day of week (mon – sun)

# You can use ‘,’ ‘-’ to give you 1,2,3,5 or 1-5 etc

# The * is used as a wildcard i.e every month

# m h dom mon dow command

# * * * * * /command

Examples;

each of the 6 command requires a space between them, here are a few examples;

### runs cron 2 mins past every hour ###

2 * * * * /home/martin/bin/play_sound.sh

### runs cron every 2 minutes ###

# m h dom mon dow command

*/2 * * * * /home/martin/bin/play_sound.sh

#01 * * * * root run-parts /etc/cron.hourly

#The run-parts script is simple enough: it just runs all the executables in the specified directory

### runs cron every fri at 15:30 ###

# m h dom mon dow command

30 15 * * * fri /home/martin/bin/backup.sh

My First Script

26, May 2009

This is not my first script but I was asked by a friend to start him off in the right direction so after writing this out I thought I should at least post it!

Ubuntu, for some reason unknown to me uses the Dash shell which causes problem with some scripts written in the standard Bash shell. To make things easier from the start it is better to swap over to do this open a Terminal [Applications], [Accessories], [Terminal] and type or copy & paste the following;

echo $SHELL

and press [Enter], you should get a response like this;

/bin/dash

which is your current Shell, the [echo] means print the following to the screen. Now lets change it to the Bash shell, type the following in the [Terminal]

sudo rm -f /bin/sh

and press [Enter], you will then be asked to enter your password as the sudo option means you are working as the root [super user] and this requires a password to make it secure. now type the following;

sudo ln -s /bin/bash /bin/sh

the first command removes the symlink [symbolic link, which Windows users will know as a shortcut] which links Dash to the sh command and the second adds a link from the bash shell to the sh command. Now type this again;

echo $SHELL

your should now get;

/bin/bash

Now lets set-up a folder in your home directory to store your scripts in and allow the system to locate them by adding the new directory to your path. Create a new folder by typing;

mkdir bin

as the [Terminal] starts up in you [home] directory the bin directory will be created there. you can list the contents of your home by typing;

ls

and [Enter] which will list all the directories [in blue] & files [in black]. Now lets add the bin directory to your path, this is where the system looks for files you ask it to launch. To see your current path, type the following.

echo $PATH

which will give you something like;

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

PATH=$PATH:/home/martin/bin

and then repeating the command;

echo $PATH

will now give you;

/home/martin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Any executable scripts you place in your bin directory can run by typing their name in a [Terminal] as we will see later. We will now create our first script using the Touch command.

touch ~/bin/my_first_script.sh

the ~/ is short for the path to your home directory i.e. /home/martin/ and the rest adds the file called my_first_script.sh in the bin directory. We need to add some commands to the file so open it in gedit by typing;

gedit /home/martin/bin/my_first_script.sh

Then copy and paste the following into the file, save it and close gedit, the first line is know as the “shebang” and point the system at the shell you are using, the lines starting ## are comments and will not be run.

#!/bin/sh
## created by Martin May 2009 ##
## say hello ##
## we give the variable today a value which represents ##
## day, date & time and prints it later by adding a $ to it ##
today=`date +%a%t%d%t%B%t%Y%t`
## ask your name and read it into a variable called name ##
echo -n “Enter your name: ”
read name
## clear the screen ##
clear
## we now give the variable time the current system time ##
time=`date +%T`
## now lets print it all out to the screen ##
echo “Hello World!”
echo
echo “Welcome ” $name
echo
echo “Today is “$today
echo
echo “and your logon time was : “$time
echo

We can now try and run the script by typing it’s name in the [Terminal];

my_first_script

which will result in;

bash: /home/martin/bin/my_first_script.sh: Permission denied

This is because the script is not yet executable, so type the following;

chmod +x ~/bin/my_first_script.sh

and run the script again with;

my_first_script.sh

It should then ask for your name and display the result with a welcome message including the time as follows;

Hello World!

Welcome Martin

Today is Tue 15 May 2009

and your logon time was : 22:33:24

Once you have this basic script running, you can start making changes and playing with the output.