I recently wrote a JSON-RPC client for Zend Framework.
The code is designed to mirror the API for Zend’s own XML-RPC client as much as possible, so if you already know how to use that then this should be really simple to just plug in and start using.
Currently HTTP is supported, though I may also add support for TCP in a future version.
I was recently asked the best way to convert a number to its textual form using PHP. For example, how to convert the number 123 to the string “one hundred and twenty-three”.
There is no built in PHP function to handle this, and the examples I found online tended to be too limited in some way or another, so I decided to write my own.
This function can accept numbers up to the system’s int size (2147483647 on 32-bit systems and 9223372036854775807 on 64-bit systems), both positive and negative, and can also handle real numbers (albeit with the inherent rounding inaccuracies involved in storing base 10 real numbers using base 2).
echo convert_number_to_words(123456789); // one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine
echo convert_number_to_words(123456789.123); // one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine point one two three
echo convert_number_to_words(-1922685.477); // negative one million, nine hundred and twenty-two thousand, six hundred and eighty-five point four seven seven
// float rounding can be avoided by passing the number as a string echo convert_number_to_words(123456789123.12345);// rounds the fractional part // one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two echo convert_number_to_words('123456789123.12345');// does not round // one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two three four five
I’ve been writing playing around with XBMC quite a bit recently, and I decided to write a JSON-RPC library in PHP for interacting with an XBMC instance from another system.
XBMC supports JSON-RPC via HTTP and TCP, and in turn xbmc-php-rpc supports both mechanisms.
I uploaded the initial release today. You can get a copy via my GitHub.
Full documentation is forthcoming, but for now the README contains instructions for getting started.
I needed to parse sets of large apache logfiles with the ability to filter results based on HTTP status code as well as URL regular expressions. Being primarily a PHP programmer my first instinct was to build this as a web based application, however I soon realised that PHP would just be far too slow when parsing large (multiple GB) files. Instead I decided to write the program as a traditional desktop application using python and GTK, utilising threads to keep it speedy and responsive. If you have any use for the program, you can download it here.
It should run on any OS assuming you have python and the GTK libraries installed. There’s no need to install, simply unzip and run parserGUI.py.
If you have ever written a CMS-type application where you accept input from users to be stored as valid XHTML, you will probably have come up against some problems!
Generally this task is accomplished by using a javascript WYSIWYG real-time editor on the client side in order to keep things simple for content editors, and the resulting markup is stored on the server. Often though, content editors tend to work in Microsoft Word and paste their content into the javascript editor. That’s where the fun begins! Windows uses its own character set (thanks Microsoft!) known as code page 1252 which, whilst being mostly compatible with the much more common latin-1 character set, is not something you generally want to use on the web – UTF-8 is a much more sensible way to go. If the content is to be stored in a database, you also need to ensure it matches the character set used by your table.
Aside from Microsoft-induced headaches, you often have little control over the markup itself. Even the best javascript editors don’t get everything 100% correct all the time, and as well as technological issues there is also potential for human error (inserting unencoded html entities for example).
All in all then, you can’t really trust the markup you receive to be valid UTF-8 encoded XHTML. I found myself in this position during the development of a CMS using CakePHP, so I decided to write a Model Behaviour which can be used to clean up strings of markup to ensure they are valid and properly encoded. It ensures that the content is free of code page 1252 characters by converting them to UTF-8, replaces any unencoded HTML entities with their properly encoded equivalents (e.g. & -> &), fixes any invalid XHTML, and cleans and tidies the source code nicely.
The behaviour’s configuration is pretty simple. You just need to specify which fields should be automatically processed before they are saved:
You can optionally specify whether to tidy the markup for each field using the PHP Tidy extension (default is true, so you only need to specify this to disable tidy):
Obviously you need to have the tidy extension available to use that feature, but the Behaviour checks for the extension and will automatically configure itself accordingly, so there is no need to explicitly disable tidy if you don’t have it installed.
private$_preProcessMap=array( // replace empty div tags with a div containing '/<div>\s*<\/div>/i'=>'<div> </div>' );
// Map of windows 1252 character points to utf-8 character points private$_cp1252Map=array( "\xc2\x80"=>"\xe2\x82\xac",/* EURO SIGN */ "\xc2\x82"=>"\xe2\x80\x9a",/* SINGLE LOW-9 QUOTATION MARK */ "\xc2\x83"=>"\xc6\x92",/* LATIN SMALL LETTER F WITH HOOK */ "\xc2\x84"=>"\xe2\x80\x9e",/* DOUBLE LOW-9 QUOTATION MARK */ "\xc2\x85"=>"\xe2\x80\xa6",/* HORIZONTAL ELLIPSIS */ "\xc2\x86"=>"\xe2\x80\xa0",/* DAGGER */ "\xc2\x87"=>"\xe2\x80\xa1",/* DOUBLE DAGGER */ "\xc2\x88"=>"\xcb\x86",/* MODIFIER LETTER CIRCUMFLEX ACCENT */ "\xc2\x89"=>"\xe2\x80\xb0",/* PER MILLE SIGN */ "\xc2\x8a"=>"\xc5\xa0",/* LATIN CAPITAL LETTER S WITH CARON */ "\xc2\x8b"=>"\xe2\x80\xb9",/* SINGLE LEFT-POINTING ANGLE QUOTATION */ "\xc2\x8c"=>"\xc5\x92",/* LATIN CAPITAL LIGATURE OE */ "\xc2\x8e"=>"\xc5\xbd",/* LATIN CAPITAL LETTER Z WITH CARON */ "\xc2\x91"=>"\xe2\x80\x98",/* LEFT SINGLE QUOTATION MARK */ "\xc2\x92"=>"\xe2\x80\x99",/* RIGHT SINGLE QUOTATION MARK */ "\xc2\x93"=>"\xe2\x80\x9c",/* LEFT DOUBLE QUOTATION MARK */ "\xc2\x94"=>"\xe2\x80\x9d",/* RIGHT DOUBLE QUOTATION MARK */ "\xc2\x95"=>"\xe2\x80\xa2",/* BULLET */ "\xc2\x96"=>"\xe2\x80\x93",/* EN DASH */ "\xc2\x97"=>"\xe2\x80\x94",/* EM DASH */ "\xc2\x98"=>"\xcb\x9c",/* SMALL TILDE */ "\xc2\x99"=>"\xe2\x84\xa2",/* TRADE MARK SIGN */ "\xc2\x9a"=>"\xc5\xa1",/* LATIN SMALL LETTER S WITH CARON */ "\xc2\x9b"=>"\xe2\x80\xba",/* SINGLE RIGHT-POINTING ANGLE QUOTATION*/ "\xc2\x9c"=>"\xc5\x93",/* LATIN SMALL LIGATURE OE */ "\xc2\x9e"=>"\xc5\xbe",/* LATIN SMALL LETTER Z WITH CARON */ "\xc2\x9f"=>"\xc5\xb8"/* LATIN CAPITAL LETTER Y WITH DIAERESIS*/ );
// Map of utf-8 chracter points to special html entities private$_entMap=array( "\xe2\x80\x98"=>'‘', "\xe2\x80\x99"=>'’', "\xe2\x80\x9c"=>'“', "\xe2\x80\x9d"=>'”', "\xe2\x82\xac"=>'€', "\xe2\x80\xa6"=>'…' );
/*
For reference, these are other entity replacement codes which might be useful one day
array(
"\xe2\x80\x9a" => '‚', // Single Low-9 Quotation Mark
"\xe2\x82\xac" => '€', // Euro sign
"\xc6\x92" => 'ƒ', // Latin Small Letter F With Hook
"\xe2\x80\x9e" => '„', // Double Low-9 Quotation Mark
"\xe2\x80\xa6" => '…', // Horizontal Ellipsis
"\xe2\x80\xa0" => '†', // Dagger
"\xe2\x80\xa1" => '‡', // Double Dagger
"\xcb\x86" => 'ˆ', // Modifier Letter Circumflex Accent
"\xe2\x80\xb0" => '‰', // Per Mille Sign
"\xc5\xa0" => 'Š', // Latin Capital Letter S With Caron
"\xe2\x80\xb9" => '‹', // Single Left-Pointing Angle Quotation Mark
"\xc5\x92" => 'Œ', // Latin Capital Ligature OE
"\xe2\x80\x98" => '‘', // Left Single Quotation Mark
"\xe2\x80\x99" => '’', // Right Single Quotation Mark
"\xe2\x80\x9c" => '“', // Left Double Quotation Mark
"\xe2\x80\x9d" => '”', // Right Double Quotation Mark
"\xe2\x80\xa2" => '•', // Bullet
"\xe2\x80\x93" => '–', // En Dash
"\xe2\x80\x94" => '—', // Em Dash
"\xcb\x9c" => '˜', // Small Tilde
"\xe2\x84\xa2" => '™', // Trade Mark Sign
"\xc5\xa1" => 'š', // Latin Small Letter S With Caron
"\xe2\x80\xba" => '›', // Single Right-Pointing Angle Quotation Mark
"\xc5\x93" => 'œ', // Latin Small Ligature OE
"\xc5\xb8" => 'Ÿ', // Latin Capital Letter Y With Diaeresis
);
*/
// apply the pre-process map $string=preg_replace(array_keys($this->_preProcessMap),$this->_preProcessMap,$string);
// apply the windows > utf8 map $string=str_replace(array_keys($this->_cp1252Map),$this->_cp1252Map,$string);
// get rid of any existing html entities to avoid double encoding $string=html_entity_decode($string,ENT_QUOTES,'UTF-8');
// break out any PHP sections since they should not be touched $parts=preg_split('/(<\?.+?\?>)/us',$string,-1, PREG_SPLIT_DELIM_CAPTURE);
// replace &, ", ', < and > with their entities, but only where they are not // part of an html tag or a comment $string=''; foreach($partsas$part){ if(false===mb_strpos(trim($part),'<?')){ $string.=preg_replace_callback( '/(?<=\>)((?*[a-z][^>]*[>])[^<])+/ius', create_function( '$matches', 'return htmlspecialchars($matches[0]);' ), $part ); }else{ $string.=$part; } }
// apply the utf-8 > entities map $string=str_replace(array_keys($this->_entMap),$this->_entMap,$string);
// trim whitespace from the end of each line and add a nice \n // tinymce in particular seems to have a bug where it will insert spaces // at the end of lines - this can cause problems with things like Revision // Behavior as the values of some fields will never be the same so a revision // is always saved even if the data itself has not changed. $parts=preg_split("/[\r\n]+/u",$string); foreach($partsas&$part){ $part=rtrim($part); } $string=implode("\n",$parts);
Sometimes it is useful to be able to associate an authenticated user with an action carried out on particular database table entry. For example, if you have a collection of recipes in a database which can be soft-deleted (i.e., marked as deleted but not actually removed from the database in order to allow for undo functionality), you might want to keep track of which user deleted a particular recipe. This is relatively easy to do in CakePHP; you can simply use association aliases to link a particular User to the deletion action by adding a corresponding foreign key to the Recipe table and defining the association accordingly:
This is all well and good, but you have to manage the foreign keys for each of these associations somehow, which would typically involve either adding a hidden form field to the appropriate view containing the currently authenticated user’s id, or using the beforeSave model callback to add the user’s id into the data set. This can soon get cumbersome if you have many models which you need to trace in this way. To solve this issue I wrote a model behaviour which automates the process of associating the user performing an action with the model.
The behaviour works via a system of trigger and target fields. When the value of a trigger field is set, the currently authenticated user is automatically associated with the model using an alias which corresponds to the action being performed. If the value of a trigger field should be ‘unset’ (more on what constitutes unset in a minute), the association will be empty.
To clarify with an example, if you have a TINYINT(1) field named ‘deleted’ which indicates the soft-deletion state of a recipe (1 = deleted, 0 = not deleted), the behaviour will automatically provide an association with the user who performed the deletion, using an alias ‘DeletedBy’. The result of a find call to the Recipe model where the recipe’s deleted field = 1 would then be as follows:
Note that the association is still present, but it contains no data, because no user deleted the recipe.
When determining if the value of a trigger field such as ‘deleted’ is set or unset, the behaviour uses two mechanisms. The first is simply to test if the value is empty according to PHP’s empty() function – if it is, the value is considered unset, otherwise it is considered set. The second mechanism is to check the value against a list of values which would not be considered empty according to PHP’s empty() function, but which you want to treat as unset anyway. An example of this would be an empty datetime string (“0000-00-00 00:00:00″); if it represents the date a recipe was deleted for example, it should be considered unset. By default the behaviour is set up to treat empty datetime strings as unset, but other arbitrary values can be added to this.
When a trigger field is determined to be unset, the target field’s value is set to 0 by default. This can be configured on a per-model basis.
The behaviour can handle any number of associations simultaneously – you might want to keep track of who created or modified a particular row for example. There really is no limit, as long as you define a trigger field and a corresponding target field the behaviour will automatically handle the associations.
By default the behaviour will use the trigger fields ‘created’, ‘modified’, ‘deleted’, ‘hidden’ and ‘locked’, using the target fields ‘created_by’, ‘modified_by’, ‘deleted_by’, ‘hidden_by’ and ‘locked_by’ (though these defaults can be easily configured). Extra trigger and target fields can be set on a per-model basis by defining them when adding the behaviour to the model (note that you will also need to re-include any default fields which you may need):
There are a few other settings which can be defined here too:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public$actsAs=array( 'Traceable'=>array( // The name of the user model used for Authentication. Defaults to 'User' 'user_model'=>'User', // An array of trigger => target fields to trace for this model. 'fields'=>array( 'created'=>'created_by', 'modified'=>'modified_by', 'published'=>'published_by' ), // The value to which target fields will be set when the corresponding trigger field is unset. // Defaults to 0. 'restored_value'=>0 );
<?php /**
*
* Automagically adds the logged in user's id to the specified fields when certain
* events occur within the model.
*
* NB: If this behavior is not working, be sure you are not using saveField to save the
* trigger field. If you do, no other fields (including the target field) will be saved.
* Instead use a standard model->save call. This is a cake issue so nothing can be
* done without altering the core.
*
* This works on a system of trigger fields and target fields. When a 'trigger' field
* is saved, and it updates a corresponding 'target' field. For example, if you have
* a trigger field called 'deleted' with a corresponding target field of 'deleted_by',
* when deleted is saved with an empty value (anything which causes empty() to return true)
* then the deleted_by field is set to a specified value (by default, 0). When deleted
* is saved with a non-empty value, deleted_by is updated with the id of the currently
* Authed user.
*
* When using find queries on a model which implements this behaviour, a
* user model will be generated for each target field and stored under a corresponding
* name in the data array (assuming the recursive level permits of course).
* For example, a User model for user identified in the created_by field will
* be stored under the CreatedBy key in the returned data array:
*
* Array (
* 'Webpages' => Array (
* 'id' => 1
* // etc
* ),
* 'CreatedBy' => Array (
* 'id' => 1,
* 'username' => 'Karl'
* // etc
* )
* )
*
* In the event that no user model can be associated with the target field (for
* example, of the target field contains 0 or NULL), the target field data will
* be set to an empty array:
*
* Array (
* 'Webpages' => Array (
* 'id' => 1
* // etc
* ),
* 'CreatedBy' => Array ()
* )
*
* @author Karl Rixon <karl@karlrixon.co.uk>
* @version 1.0
*
**/ class TraceableBehavior extends ModelBehavior {
/**
* @var mixed An array of default configuration options
* @access private
*/ private$_defaults=array( // The name of the user model. 'user_model'=>'User', // An array of fields, with a trigger field as key and a target field as value. // When the trigger field is set to a non empty() value, the user's id is // inserted into the corresponding target field. When it is set to an empty() // value, the target field is reset to restored_value. 'fields'=>array( 'created'=>'created_by', 'modified'=>'modified_by', 'deleted'=>'deleted_by', 'hidden'=>'hidden_by', 'locked'=>'locked_by' ), // The value to which target fields will be set when toggled back. 'restored_value'=>0, // If true, a user model representing each target field will be automatically bound // to the model which implements this behaviour (using a belongsTo association). 'auto_bind'=>true, 'map'=>array() );
/**
* @var int Stores the id of the currently Authed user.
* @access private
*/ private$_userId=0;
/**
* @var mixed An array of values which should indicate that a field has been emptied. There
* is no need to specify the values which evaluate true using PHP's empty() function;
* this is intended for custom values which would not normally be considered empty().
* @access private
*/ private$_empty=array( '0000-00-00 00:00:00'// empty datetime );
private$_noTrace=false;
/**
* Initialises the behaviour.
*
* @param Model $model A reference to the model object to which this behaviour is attached.
* @param mixed $config An array of behaviour configuration options to be merged with the defaults.
* @return void
* @author Karl Rixon <karlrixon@gmail.com>
* @version 1.0
* @since 1.0
*/ function setup($model,$config=array()){
if(!$model->useTable){ // Model is not tied to a database table. return; }
/**
* Called before model data is saved.
*
* This method is just used as a place to attach a call to the _trace method which
* does all the hard work of managing the trigger/target fields.
*
* @param Model $model A reference to the model object to which this behaviour is attached.
* @return boolean Always returns true to allow the save to continue as normal.
* @author Karl Rixon <karlrixon@gmail.com>
* @version 1.0
* @since 1.0
*/ function beforeSave($model){ if(!empty($this->settings[$model->alias]['map'])){ $this->_trace($model); } returntrue; }
/**
* Cleans up the found data, changing empty association models to empty arrays.
*
* Without this, any empty association model data will contain all of the keys
* from the User model's table. It seems neater to return an empty array for any
* items which do not have a matching user model.
*
* @param Model $model A reference to the model object to which this behaviour is attached.
* @param mixed $results An array containing the data returned from the find.
* @return void
* @author Karl Rixon <karlrixon@gmail.com>
* @version 1.0
* @since 1.0
*/ function afterFind($model,$results,$primary){
/**
* Checks for any triggered fields, and sets the corresponding target field accordingly.
*
* If a triggered field is found in the data, and it's value is empty (anything
* which evaluates to true using PHP's empty() function), its target field is
* set to the restored_value. If it is not empty, its target field is set to the
* id of the currently Authed user.
*
* @param Model $model A reference to the model object to which this behaviour is attached.
* @return bool True if succesful, false if an error occured. Note that true will be
* returned even if no trigger fields were found. False will only be returned if
* an actual error occured - the lack of trigger fields is not considered an error.
* @author Karl Rixon <karlrixon@gmail.com>
* @version 1.0
* @since 1.0
*/ privatefunction _trace(&$model){
/**
* Gets the id of the currently Authed user.
*/ privatefunction _getAuthedUserId($model){
App::import('Component','Session'); $session=new SessionComponent(); return$session->read('Auth.'.$this->settings[$model->alias]['user_model'].'.id'); }
/**
* Builds an array with field names as keys, and camelized versions of
* the field names as values.
*/ privatefunction _buildFieldMap($model){
// Get an array of all fields which exist in both the model's table, and in // the behaviour settings for this model. $fields=array_values(array_intersect( $this->settings[$model->alias]['fields'], array_keys($model->_schema) ));
We use analog at work to perform analysis of our Apache server logs. I was setting up a new server recently and after installing and configuring analog I found I was getting an error every time I tried to run analog against our logfiles. The error was as follows:
1
Warning F: Failed to open logfile /var/log/httpd/access_log: ignoring it
If you are having a similar problem, hopefully this post will help you solve it.
There are a couple of basic causes of this error. The first and most obvious is that analog genuinely can’t open the logfile(s) in question because it either doesn’t exist at the location specified, or else it exists but cannot be opened by analog due to insufficient permissions. The second is that the logfile in question is too large to be processed (exactly what constitutes “too large” varies, but it typically means greater than either 2GiB or 4GiB on a 32bit system).
File doesn’t exist, or has insufficient permissions
This situation is easy to test for. First ensure you are logged in as the user who runs analog when it gives the error (if you run it manually you are fine, but if you run it via another user’s crontab for example you need to log in as that user to test). Next enter the following command (assuming analog is failing to open /var/log/httpd/access_log):
1
head/var/log/httpd/access_log
If you get the first few lines of your logfile printed to the terminal, then you don’t have a permissions error. Skip to the next section.
If you get an error indicating that the file could not be found, then you need to adjust the LOGFILE line(s) in your analog.cfg (typically located in /etc/analog.cfg) to point to the real location of your logfile(s).
If you get an error indicating that you have insufficient permissions, you will either need to run analog as a user who does have the correct permissions, or else alter the permissions of the logfile so that you can execute the command successfully. Remember that although the file itself may have adequate permissions, the entire path to the file also needs to have adequate permissions. On my CentOS system for example, the logfile at /var/log/httpd/access_log is readable by everyone, but the /var/log/httpd directory is only readable by root, which means that running analog on this logfile as any user other than root would fail.
If you need to alter permissions, it is only necessary to make the file readable, so the following is sufficient:
1
sudochmod a+r /var/log/httpd/access_log
Again be sure to repeat this for any parent directories of your logfile which are unreadable.
Log files are too large
This is the issue I had, and it caused me half an hour or so of frustration trying to find the problem! Luckily it is simple enough to fix. The cause of this issue is that the logfiles are not rotated frequently enough and grow too large. In my case, httpd logs were set to rotate at the default 1 week. This is fine for most sites, but it happens that the site I am working on is fairly busy, and a week’s worth of access logs amount to almost 5GiB. To adjust the log rotation settings, I use logrotate, which is configured via files placed in /etc/logrotate.d on my system. The file which needs editing in this case is /etc/logrotate.d/httpd. I won’t go into the details of logrotate settings here, but I will give an example of my configuration. I set it to rotate logs daily, and keep 28 days worth of historical logs:
All future logfiles should now be well under the OS file size limit. However if you have existing logfiles which you would like analog to be able to process but are too big, you can split them down into smaller files. To do this, use wc to find the number of lines in your file (I’m using a file named access_log.1 in this example):
1
wc-l access_log.1
That should give you something like:
1
16542827 access_log.1
Next you need to decide into how many chunks you would like to split the file. My file is almost 5GiB, so I’m going to split it into 5 chunks. Take the number of lines returned by wc and divide it by the number of chunks (it doesn’t have to be exact). This gives me roughly 3300000 lines per file, so that’s the figure I’ll use for splitting:
1
split-l3300000 access_log.1
This may take a while for large files, but once it’s done you should now find that you have some new files along with your old access logs:
As you can see I actually ended up with 6 new files because I rounded the number of lines at which to split to 3300000. The last new file contains the leftovers. At this point you can archive your original large logfile and rename your new files to match your log naming scheme:
I upgraded to Ubuntu 10.10 and found that Firefox was crashing every time I tried to start it. This is caused by the icedtea plugin. Removing the plugin package fixed the problem.
By default, Virtualbox uses NAT to provide network support for guest operating systems. This is fine if you just want to have basic network functionality such as internet access, but it means that the guest will be on its own network. Often having the guest machine on the same LAN as the host is desirable (if you want to connect to the guest via SSH for example).
This is actually very simple to set up. Just open a terminal on the host and install the necessary packages:
1
sudoapt-get install bridge-utils uml-utilities
Next open Virtualbox, select your guest and click Settings. Change ‘Attached to’ from NAT to Bridged Adapter, and click OK.
If you are using DHCP, that’s it. If you are using a static IP, you will need to configure the guest OS accordingly so that it can join your LAN.
I tend to use ruTorrent as a front-end for rTorrent and so I like to have rTorrent start silently and automatically on boot. Most of the guides on this subject seem to suggest using an init script placed in /etc/init.d, but this is more complicated than it needs to be in my opinion. You can use start-stop-daemon instead which is much simpler and yields the same result. Here’s how.
First install dtach:
1
sudoapt-get install dtach
Then edit your /etc/rc.local file:
1
sudonano/etc/rc.local
By default this file looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.
exit0
Add the following before exit 0, making sure to replace karl with the user as which you would like to run rTorrent, and /usr/local/bin/rtorrent with the location of rTorrent on your system (/usr/bin/rtorrent if you installed from the repo, /usr/local/bin/rtorrent if you compiled rTorrent from source with the default install path):
1
start-stop-daemon --start--chuid karl --name rtorrent --exec/usr/bin/dtach ---n/tmp/rtorrent.dtach /usr/local/bin/rtorrent
Your /etc/rc.local should now look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.
start-stop-daemon --start--chuid karl --name rtorrent --exec/usr/bin/dtach ---n/tmp/rtorrent.dtach /usr/local/bin/rtorrent
exit0
Press ctrl+o to save the file and ctrl+x to quit nano.
That’s it! To test you can either reboot, or else manually run the rc.local script:
1
sudo/etc/rc.local
If you don’t use an rTorrent front end, or want to view rTorrent in a terminal for whatever reason, you can reattach it to your terminal with the following command:
1
dtach -a/tmp/rtorrent.dtach
You can either press ctrl+z or simply close the terminal to quit dtach (rTorrent will keep running in the background).