Looks like I'm having the same problem on debian jessie with systemd and virtualbox 5.0.10 packaged from Oracle.
After lot of debugging there seams to be something related to the way the start-stop-daemon is called in the stop() function in /usr/lib/virtualbox/vboxautostart-service.sh (around line 236) which does not triggers for the start() function in the same way
What I came up with is an horrible hack but really would like someone more systemd friendly to dig into this issue.
I replaced in /usr/lib/virtualbox/vboxautostart-service.sh :
for user in `ls $VBOXAUTOSTART_DB/*.stop`
do
start_daemon `basename $user | sed -ne "s/\(.*\).stop/\1/p"` $binary $PARAMS > /dev/null 2>&1
done
with :
for user in `ls $VBOXAUTOSTART_DB/*.stop`
do
su - `basename $user | sed -ne "s/\(.*\).stop/\1/p"` -c "$binary $PARAMS" > /dev/null 2>&1
done
I believe the addition of the Restart
option to your systemd
service file would ensure it gets start when not stopped by the systemctl stop
command:
Restart=[no | on-success | on-failure | on-abnormal | on-watchdog | on-abort | always ]
Info:
no: the default, the service will not be restarted
on-success: will be restarted only when the service process exits cleanly
on-failure: will be restarted when the process exits with a non-zero exit code
on-abort: will be restarted only if the service process exits due to an uncaught signal not specified as a clean exit status
on-watchdog: will be restarted only if the watchdog timeout for the service expires
always: will be restarted regardless of whether it exited cleanly or not, got terminated abnormally by a signal, or hit a timeout
Restart=
Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached. The service process may be the main service process, but it may also be one of the processes specified with ExecStartPre=, ExecStartPost=, ExecStop=, ExecStopPost=, or ExecReload=. When the death of the process is a result of systemd operation (e.g. service stop or restart), the service will not be restarted. Timeouts include missing the watchdog "keep-alive ping" deadline and a service start, reload, and stop operation timeouts.
Takes one of no, on-success, on-failure, on-abnormal, on-watchdog, on-abort, or always. If set to no (the default), the service will not be restarted. If set to on-success, it will be restarted only when the service process exits cleanly. In this context, a clean exit means an exit code of 0, or one of the signals SIGHUP, SIGINT, SIGTERM or SIGPIPE, and additionally, exit statuses and signals specified in SuccessExitStatus=. If set to on-failure, the service will be restarted when the process exits with a non-zero exit code, is terminated by a signal (including on core dump, but excluding the aforementioned four signals), when an operation (such as service reload) times out, and when the configured watchdog timeout is triggered. If set to on-abnormal, the service will be restarted when the process is terminated by a signal (including on core dump, excluding the aforementioned four signals), when an operation times out, or when the watchdog timeout is triggered. If set to on-abort, the service will be restarted only if the service process exits due to an uncaught signal not specified as a clean exit status. If set to on-watchdog, the service will be restarted only if the watchdog timeout for the service expires. If set to always, the service will be restarted regardless of whether it exited cleanly or not, got terminated abnormally by a signal, or hit a timeout.

Image source
Source:
https://www.freedesktop.org/software/systemd/man/systemd.service.html
Best Answer
Your
.service
file should look like this:Now, take a few more steps to enable and use the
.service
file:Place it in
/etc/systemd/system
folder with say a name ofmyfirst.service
Make sure that your script executable with:
Start it:
Enable it to run at boot:
Stop it:
Notes
You don't need to launch Spark with
sudo
in your service, as the default service user is already root.Look at the links below for more
systemd
options.Moreover
Now what we have above is just rudimentary, here is a complete setup for spark:
To setup the service:
Further reading
Please read through the following links. Spark is a complex setup, so you should understand how it integrates with Ubuntu's init service.