2

I'm trying to understand the init process.

I have a script which launches 3 background processes (2 shell scripts and a java app). It works just fine from the command line.

I'd like to execute that script at startup.

/etc/init.d/skeleton didn't make sense to me because it was looking for a single process it seemed. And most init scripts seems so complex I'm getting sort of lost.

All I really want to do is:

#!/bin/bash
/opt/myapp/bin/my_start_script

If that just happened at startup I'd be very happy.

2 Answers 2

4

Use Upstart as the System V Init scripts are being deprecated and its simpler.

Simply place this file my_script.conf into /etc/init

# Start when pc starts up
start on runlevel [2345]
# Stop when pc shuts down 
stop on runlevel [016]

# Start Script 
exec my_start_script
# Stop Script 
pre-stop my_stop_script

The pre-stop will script contain code to kill your 3 process and is optional, when the service is stopped sigterm will be sent to the my_start_script process but depending on your script it may already have exited.

You can test your new service by running sudo service my_script start and sudo service my_script stop

1
  • Are you sure it works in Ubuntu 16.04 too? I am really having troubles with that. Jul 3, 2019 at 10:51
3

You can add your link to the Startup Applications application in Ubuntu if you want to use the GUI. The downside of this is that it will only execute when you log in to the GUI (i.e. not for other users, and not if you don't log in to the Ubuntu Desktop environment).

Using the command line, you can cause a script to run before the Ubuntu Desktop loads.

To do this, make your script executable, copy it to /etc/init.d and create a symbolic link to the script in /etc/rc2.d if you want it executed in the default multi-user runlevel (runlevel 2). You should rename it with an S followed by the order you want it executed in, respective to the other links in there.

  1. Make script executable:

    sudo chmod u+x myscript.sh

  2. Copy it to /etc/init.d:

    sudo cp myscript /etc/init.d

  3. Create a symlink in rc2.d:

    cd /etc/rc2.d

    sudo ln -s /etc/init.d/myscript.sh

  4. Rename it according to the naming scheme:

    sudo mv myscript.sh S70myscript.sh

For more information, see the README files in /etc/init.d and /etc/rc2.d.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .