Friday, January 5, 2007

Howto: get Azureus to start at boot and run headless (ie without a gui) on Linux

Getting Azureus to start at boot headless saves system resources and means it will work away nicely in the background. This approach works great with a web front-end like the Azureus Swing UI. To get the most out of bittorrent, you also need to make sure that it shuts down properly. Here are the steps I used to set it up...

Install and configure Azureus
  • Download Azureus and decompress it into a directory
  • Download log4j.jar and commons-cli.jar and put them in the directory you uncompressed Azureus into
  • Run Azureus by opening a terminal, changing into the directory you put Azureus and typing
./azureus
  • Configure Azureus with the GUI tool
  • Exit Azureus
  • Create a file called headless.sh with the following in it:
#!/bin/bash
java -jar Azureus2.jar --ui=console >/dev/null 2>&1
  • Make that file executable by typing
chmod u+x headless.sh
  • Create a file called azureus_rcscript with the following in it: (you need to change the AZ_USER and DIR variables to represent the name of the user who will be running Azureus and the directory Azureus is installed to)
#! /bin/sh
#rc script for Azureus by Phill - http://phillstechstuff.blogspot.com
#Based on script from Azureus wiki - http://www.azureuswiki.com/index.php/HeadlessSwingUIAtBoot

#The user that will run Azureus
AZ_USER=user

#your path to the azureus directory, where Azureus2.jar is located
DIR=/home/user/azureus

#executable files in the following paths that are perhaps needed by the script
PATH=/bin:/usr/bin:/sbin:/usr/sbin

#Description
DESC="Azureus daemon"

case "$1" in
start)
if [[ ` ps -ef | grep "java -jar Azureus2.jar --ui=console"|grep -v grep` ]]
then
echo "Azureus is already running!"
else
echo "Starting $DESC"
su $AZ_USER -c "cd $DIR; ./headless.sh"
fi
;;
stop)
if [[ ` ps -ef | grep "java -jar Azureus2.jar --ui=console"|grep -v grep` ]]
then
echo -n "Stopping $DESC"
su $AZ_USER -c "kill `ps -ef | grep "java -jar Azureus2.jar" | grep -v grep | awk '{print $2}'`"
echo " ... done."
else
echo "Coulnd't find a running $DESC"
fi
;;
restart)
if [[ ` ps -ef | grep "java -jar Azureus2.jar --ui=console"|grep -v grep` ]]
then
echo -n "Stopping $DESC"
su $AZ_USER -c "kill `ps -ef | grep "java -jar Azureus2.jar" | grep -v grep | awk '{print $2}'`"
echo " ... done."
else
echo "Coulnd't find a running $DESC"
fi
echo "Starting $DESC: $NAME"
su $AZ_USER -c "cd $DIR; ./headless.sh"
echo " ... done."
;;
status)
if [[ ` ps -ef | grep "java -jar Azureus2.jar --ui=console"|grep -v grep` ]]
then
echo "Azureus is RUNNING"
else
echo "Azureus is DOWN"
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac

exit 0

  • Put that file inside the startup for the runlevel your machine runs at. For my machine, that is runlevel 5, and it is probably the same for yours if it boots up to a graphical environment. I did this by typing
su
mv azureus_rcscript.sh /etc/rc5.d/S99azureus_rcscript.sh
cp /etc/rc5.d/S99azureus_rcscript.sh /etc/rc5.d/K01azureus_rcscript.sh
chmod 755 /etc/rc5.d/S99azureus_rcscript.sh /etc/rc5.d/S99azureus_rcscript.sh
  • The S99 makes it run this script after everything else has started up, which is good as it makes sure it won't try to run before things like network services have started. As the script also includes functionality to shut down Azureus, I copied it to have it run when we leave runlevel 5, which usually means shutdown time. The K01 means to run it before shutting down any other processes
That's it! If you reboot you should now have Azureus running in headless mode, and shutting down cleanly when you shutdown the system.

Please let me know if this works for you, and if you have any questions!

12 comments:

David said...

Hi Phil,

Just tried this script out. Seems to launch azureus great (i can connect through html webui), but my linux box hangs during system startup. I get the message

"Starting azureus_rcscript.sh: Starting Azureus daemon", but it never goes to OK.

Any thoughts?

Phill said...

Thanks for the comment David. My first thoughts are:

The line it is hanging on is probably:
su $AZ_USER -c "cd $DIR; ./headless.sh"

Have you set the AZ_USER to a user that exists and has permission to run Azureus and DIR to the directory containing azureus? If you're using your own account (eg. david) and have decompressed all the files as that user then having something like:

AZ_USER=david
DIR=/home/david/azureus

should work.

If the above has been set correctly and it still isn't working then I'll need more information. It would help if you could give me an "ls -l" of your /etc/rc5.d/S99azureus_rcscript.sh, and also let me know if running "/etc/rc5.d/S99azureus_rcscript.sh
start" and "/etc/rc5.d/S99azureus_rcscript.sh stop" works

Thanks again for the feedback.

John Jacklitsch said...

Hello phill,

It's been quite awhile since this posting. Are you still involved with this topic. I'm having the same problem as david had.

UniIsland said...

hi Phill,

this is a good alternative script to the official screen method, running azureus as a daemon

but do you think azureus can shutdown "tidily" when the process is simply killed?

Louis said...

Killing the java process with just the plain kill command gives it a chance to close properly. If the script called "kill -9" then no, it wouldn't close nicely.

P.S. Thanks Phill. The script works great for me in slackware (with some mods). I changed it so that it didn't rely on headless.sh for launching the program but instead just did it directly inside the script.

I've posted my modified version here:
http://docs.google.com/View?id=ddt54qhw_1f5hcwbg6

Anyone know what I need to change to make restart work properly?

Louis said...

Azureus Daemon in Slackware

Making clicky link.

Bferd said...

I keep getting this error

/etc/init.d/azureus_rcscript.sh: 65: [[: not found

UniIsland said...

@Bferd
maybe line break at wrong place.

Orban said...

Write this in the headless.sh file

java -jar Azureus2.jar --ui=console < /dev/null >>logfile.txt 2>>errorfile.txt&

And the "Starting azureus_rcscript.sh: Starting Azureus daemon" problem on boot disappear.

1lias3 said...

to avoid the kill, another approach is to use the telnetUI as below:

start azureus as with :

--ui=telnet (instead of --ui=console)

it will start a telnet server, for example:

changeLocale: no message properties for Locale 'English (United States)' (en_US), using 'English (default)'
Telnet server started. Listening on port: 57006


you have telnet access now (telnet 0 57006) to manage azureus

and to stop it, there is supported command "quit iamsure" which will stop (not kill) the app in regular way!

this is what i decided to use, hope it helps if you are interested :)

Abd Halim Haris said...
This comment has been removed by the author.
Abd Halim Haris said...

I get the [[: not found
my fix is change
#! /bin/sh
to
#! /bin/bash