Archive for the ‘ubuntu’ Category

Bash Script: Time, Tar, Date & Email

As im trying to learn bash scripting its easier if you have a little idea of a script that can be useful to you. For this example I want to backup a set of files/directories and have the script email me with the date, contents of the tar and the time it took to run the process…not as easy as it looks due to the “time command” outputting to stderr. The script can then be used as a cron job.

#!/bin/bash

set -e
set -u

DATESTAMP=`date ‘+%d_%m_%Y-%H%M’` #Date will appear in day_month_year-hour&min format
EMAIL=”user@uni.edu.au” #Define an email address for mail to use
SUBJECT=”Backup completed for PC-HTPC $DATESTAMP” #Create a subject for the mail including the date defined above
ARCHIVE=”backup_$DATESTAMP.tar.gz” #Create a name for the tarfile including the date defined above
BODY=`(time tar -cvpzf $ARCHIVE test*) 2>&1`#The tar command (including being timed) and redirecting stderr to stdout

echo -e “$ARCHIVE\n\n$BODY” | mail -s “$SUBJECT” “$EMAIL” #Using the variables archive and body and using the mail command to send the email

Advertisements

How launch XBMC with MCE Remote “Home” Button on Ubuntu

Having recently installed ubuntu on my HTPC I was keen to get my Microsoft MCE Remote Control working. Initially this is easy to setup with LIRC but its a little trickier to get the buttons mapped the way you want. There is a lot of documentation on the web which doesnt seem to work.

I following the guide here with a few tweaks of my own: http://www.hackourlives.com/setup-windows-media-center-remote-for-mythtv-and-xbmc/

Im only interested in launching XBMC with the “Home” button on the remote, the power button will close XBMC with the default setup.

My setup:

Microsoft MCE Remote with USB IR Receiver
HTPC: Dell Hybrid
Version of ubuntu:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION=”Ubuntu 9.10″

Here are the steps:

1: Install the LIRC package which supports IR remotes for linux

$ sudo apt-get install lirc lirc-modules-source

You will then next to select the correct remote in the GUI which pops up.

2: For basic setup for media centre remote integration with xbmc and mythtv etc install the following:

$ sudo apt-get install mythbuntu-lirc-generator

and run in a terminal:

$ mythbuntu-lircrc-generator

According to the source this creates a .lirc directory and .lircrc file. However in my experience it created a txt file .lirc and no .lircrc. I removed the txt file .lirc and created a directory .lirc and txt file .lircrc in my home account

$ mkdir .lirc
$ touch .lircrc

3: Ensure the remote/receiver is now working, in a terminal window:

$ irw <return>

Now press some buttons on the remote such as “Home” and “Play” and you should see the button codes | names | mceusb

4: Now cd into your .lirc directory and create these two files

$ touch irexec
$ touch irxevent

5: Edit the irexec and enter the following:

# Start XBMC
begin
prog = irexec
button = Home
config = ~/scripts/startXBMC.sh &
end

6: Make another directory in your home account called scripts

$ mkdir scripts

7: cd into this directory and make two files, one called startIRexec.sh & another called startXBMC.sh

8: Edit startXBMC.sh and enter the following:

#!/bin/bash

# Test to see if XBMC is running first
if ps -ef|grep -v grep|grep -i xbmc.bin
# if ps -ef|grep -v grep|grep -i mythfrontend.re
then
# Do nothing
echo “XBMC already Running!”
else
# Startup MythTV
xbmc
fi
exit

9: Edit startIRexec.sh and enter the following:

#!/bin/bash

# Test to see if IRXevent is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -i irxevent
then
ps aux|grep -i user_name|grep -i irxevent |awk ‘{print $2}’|xargs kill
else
# Do nothing
echo “irxevent already dead!”
fi

# Test to see if IRexec is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -vi start|grep -i irexec
then
ps aux|grep -i user_name|grep -i irexec |grep -vi start|awk ‘{print $2}’|xargs kill
else
# Do nothing
echo “irexec already dead!”
fi

#test to see if an instance of irxevent is already running
if ps -ef|grep -v grep|grep irxevent
then
# do nothing
echo “irxevent already running”
else
# start irxevent
irxevent ~/.lircrc &
fi

#test to see if an instance of irexec is already running
if ps -ef|grep -v grep|grep irexec
then
# do nothing
echo “irexec already running”
else
# start irxevent
irexec -d ~/.lircrc &
fi

exit

10: Edit the startIRexec.sh file and replace all user_name entries with your user name.

11: Make sure both scripts are executable:

$ chmod +x *.sh

12: Add the following lines to the .lircrc file in your home account

include ~/.lirc/irexec
include ~/.lirc/irxevent

13: Manually execute the startIRexec.sh script

$ ~/scripts/startIRexec.sh

With any luck you should now be able to launch XBMC with the “Home” button on the MCE remote 🙂

To ensure that the irexec script starts when you login:

System > Preferences > Startup Application

The application to launch is /home/username/scripts/startIRexec.sh

Enjoy your new remote!