Archive for April, 2011

Run Once Only Bash Scripts

Thursday, April 21st, 2011

I am constantly amazed by what can be achieved with a simple bash script or two. I had a situation where I needed a script to run only once, and to not get any errors spat out at me from the cli. I needed a second script to call the run once script to go into rc.local, a file I didn’t want to mess with, so the call in rc.local would just be a quick file check after the first run. for me, the clever bit is that the scriptI have to do the work can “move” (rename) itself once it has done its business, thereby ensuring it is not called upon again, but is still there if needed.

Here’s the script that does the work (called once.sh):

#!/bin/bash
Do the work

echo “I am now going to back myself up :)!”
sleep 2
mv once.sh once.sh.bak
echo file backed up
sleep 2
exit 0

The key line is the mv command, running on itself! I’ll remove the comments for the production setting

Here’s is the script that calls the one above (called runitonce.sh):

 #!/bin/bash
if [ -f ~/once.sh ]
then
echo the file exists
sleep 2
~/once.sh
else
echo the file does not exist
sleep 2
fi

Basically, if once.sh is there, this script, runitonce.sh, runs once.sh, which when finished, renames itself. If once.sh is not there, the script just finishes. Subsequent runs of the script runitonce.sh called in rc.local just run through as there is no file to run. Simples :)

Play your entire music collection - Mplayer One Liner

Saturday, April 16th, 2011

Sometimes just plain lazy, and want just to play music. Run this command in the background (ALT+F2) or in a terminal for playback of all your mp3/ogg/m4a/wma, shuffled, looped, normalised. What’s nice about this is that the shuffle algorithym does a good job and with a large collection of music you don’t get repeats. I found that trying the same thing with vlc meant the shuffle was  always the same, and so you always started with the same music. :( Anyway the one-liner:

find ~/Music \( -iname “*\.mp3″ -o -iname “*.m4a” -o -iname “*.ogg” -o -iname “*.wma” \) -exec mplayer -nocache -af volnorm -shuffle -loop 0 ‘{}’ + &

Change “~/Music” to suit.

Even better put this in a script, and link to a keyboard shortcut, or add to your startup?

Now running the one liner from a terminal takes a bit of stopping, you’ll need to CTRL+C a couple of times in quick succession to close find then mplayer. Or you can run another one-liner:

killall -15 find mplayer

If you run the command from a script, you’ll have to shut down the script first, and then kill off the running instances of find and mplayer. Best to also do this from a script as well. Say the script you use to run is called singles.sh, let’s create another called endsingles.sh which contains:

#!/bin/bash

killall -15 singles.sh
killall -15 find
killall -15 mplayer

I am sure someone clever could bind this all together in one script, based upon a keyboard shortcut toggle or something…

Prevent Screen Blanking on Xubuntu CLI / Slim / Openbox Install on Asus EB1012 nettop

Wednesday, April 6th, 2011

Normally I am able to take care of screen blanking through the gui powersaving options in Xubuntu, however my command line install followed by installing openbox and using slim as a login manager presented me with real problems. teh screen would blank and half kill the x server, leaving a reboot as the only option. I found some xset options which I put into a script and added this to autostart.sh, which seemed to take care of X issues, but the terminal blanking was still going on. Using setterm I was able to stop terminal blanking but had to find a place to put it on startup. This turned out to be in /etc/profile (for all users). So now writing this post from my non blanking setup :)

sudo nano ~/.config/openbox/noblankx.sh

#!/bin/bash

xset s blank

xset s 0 0

xset -dpms

I was able to test this by outputting xset -q > test.txt at the end of the script, which produced

Keyboard Control:
auto repeat:  on    key click percent:  0    LED mask:  00000000
XKB indicators:
00: Caps Lock:   off    01: Num Lock:    off    02: Scroll Lock: off
03: Compose:     off    04: Kana:        off    05: Sleep:       off
06: Suspend:     off    07: Mute:        off    08: Misc:        off
09: Mail:        off    10: Charging:    off    11: Shift Lock:  off
12: Group 2:     off    13: Mouse Keys:  off
auto repeat delay:  660    repeat rate:  25
auto repeating keys:  00ffffffdffffbbf
fadfffefffedffff
9fffffffffffffff
fff7ffffffffffff
bell percent:  50    bell pitch:  400    bell duration:  100
Pointer Control:
acceleration:  2/1    threshold:  4
Screen Saver:
prefer blanking:  yes    allow exposures:  yes
timeout:  0    cycle:  0
Colors:
default colormap:  0×20    BlackPixel:  0    WhitePixel:  16777215
Font Path:
/usr/share/fonts/X11/misc,/usr/share/fonts/X11/100dpi/:unscaled,/usr/share/fonts/X11/75dpi/:unscaled,/usr/share/fonts/X11/100dpi,/usr/share/fonts/X11/75dpi,/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType,built-ins
DPMS (Energy Star):
Standby: 600    Suspend: 600    Off: 600
DPMS is Disabled

The important bits for this are in bold.

Now add the script to autostart.sh

nano ~/.config/openbox/autostart.sh

and add the line:

~/.config/openbox/noblankx.sh &

Now for terminal blanking

sudo nano /etc/profile

scroll to the bottom and add

setterm -blank 0 -powersave off -powerdown 0

[EDIT] You can also put this command in /etc/rc.local, it works just the same :)

A reboot is necessary to test it out.

More information is available from man xset and man setterm but google is better to get real examples of usage.

Looking for a way to view the current settings for setterm, to see what is in place……

Call of Duty / Black Ops / Modern Warfare Disconnecting from Xbox Live on Start Up

Monday, April 4th, 2011

Off beat post this.

My sons Xbox started misbehaving the other day, and refused to run the Call of Duty games connected to XBox Live. The games would start up Ok, but then disconnect from XBox Live - not good. Googled a lot but not much out there (a lot about it disconnecting during games but not on start up.)

General advice was to clear the system cache, or wait at the dashboard for a while, open ports on the router, reset the console, recover gamertag, but these didn’t work. Other games like Team Fortress would play OK, though.

Finally I tracked down the solution, which was to set a FIXED IP Address for the XBox on the router. It had been working fine previously on DHCP, but suddenly stopped working. The fixed IP address resolved all the issues on all the games.

I am an utter cult :)