Ok, Alex, the point is that all the userspace processes in Linux are started with init process, whose pid is 1. For instance, run pstree to see the tree of your processes, whose root is init.. There are several versions of init process implementation nowadays, most notable are
- sysVinit (classical init, still used by some distributions, including older Debian)
- Upstart init, used by older Ubuntu and some RHEL (Red Hat) and older Fedora versions
- systemd init, used by modern Fedora, Ubuntu, Debian, RHEL, SUSE versions
Traditionally, Unix'es used init implementation called sysVinit init, called by the name of https://ru.wikipedia.org/wiki/UNIX_System_V version of Unix. It is very influential and other inits are backward compatible to it.
Basically, sysVinit first reads /etc/inittab file, decides, which runlevel to run and tells /etc/init.d/rc script to execute so-called init scripts. E.g. when it normally boots to a multi-user runlevel, which is usually runlevel 2 on Ubuntu, /etc/init.d/rc starts executing scripts in /etc/rc2.d. Files there are only symbolic links to scripts, while the scripts themselves are stored in /etc/init.d directory. The naming of those symlinks in /etc/rc*.d directories is as follows. Say, we've got the following scripts in /etc/rc2.d:
$ls /etc/rc2.d
S16rsyslog
S17apache2
K02network-manager
It means, that upon switching to runlevel 2 init process first kills network-manager processes, cause its script name starts with K - K02network-manager and then starts processes, whose names start with S. The two digits after S or K is the number from 00 to 99, which determines the order, the processes are started in. E.g. rsyslog is started before apache2, because 16 is less than 17 (that makes sense, cause you want apache to rely upon rsyslog's logging capacities, thus rsyslog should be started first). The scripts are casual shell scripts, executed by #!/bin/sh.
So, basically to start a program upon startup in sysVinit style, write your own script (copy-pasting it from any example, you've got in /etc/init.d), put it to /etc/init.d and create a symlink to it under a reasonable name, e.g.
S99mytrojan in /etc/rc2.d.
Here's an explanation of typical sysVinit scripts in /etc/init.d http://docs.oracle.com/cd/E19683-01/806-4073/6jd67r96g/index.html
Now, Ubuntu guys decided that they want additional functionality from init.
They wanted a fast booting OS, so they wanted their scripts to be executed in parallel; they wanted dead processes to be automatically restarted; they wanted the processes to invoke each other in an explicit manner by events (so that apache is run by "syslog started" event, and syslog is run by "file systems mounted" event etc., so we have events instead of some numbers 00-99). Thus, they've made Upstart and here is how it works. Upstart initscripts are put in /etc/init directory (don't confuse with /etc/init.d). Upstart usually runs /etc/init.d/rc too, so it's gonna execute your sysVinit scripts normally. But if you want your script to be respawned upon exit - Upstart events are for you.
Although I can't check that my script is working, I suppose, that for your aims, you should write the following /etc/init/mytrojan.conf script:
start on runlevel [02]
respawn
exec mytrojan --argument X
But if you need dependencies, at least filesystems and network, may be it makes sense to replace start on runlevel [02] with something like:
start on (local-filesystems and net-device-up IFACE!=lo)
WARNING: I didn't check the correctness of this, cause I can't. Especially, I'm not quite sure about how to start script after your network connection is up and running (I used this version). Try googling for "upstart on network up".