i want to create a autostart script that starts whenever the server starts.

mkdir /scripts
vim /scripts/start.sh
#!/bin/bash
echo "========= starting up docker service"
systemctl start docker

echo "========= starting up docker containers"
docker start containername

vim /scripts/stop.sh
#!/bin/bash
echo "========= stop all docker containers"
docker stop $(docker ps -q); # stop all running containers

echo "========= stop docker service"
systemctl stop docker

vim /etc/systemd/system/onboot.service

# fill with
[Unit]
Description=onboot
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/scripts
ExecStart=/scripts/start.sh
Restart=on-abort

[Install]
WantedBy=multi-user.target


# :wq save and quit

# test service
systemctl status onboot
● onboot.service - onboot
Loaded: loaded (/usr/lib/systemd/system/onboot.service; enabled; vendor preset: disabled)
Active: inactive (dead)

# test start
systemctl start onboot
● onboot.service - onboot
   Loaded: loaded (/usr/lib/systemd/system/onboot.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-08-09 18:36:01 CEST; 3s ago
  Process: 7594 ExecStop=/scipt/stop.sh (code=exited, status=203/EXEC) <- typo
  Process: 7592 ExecStart=/scipt/start.sh (code=exited, status=203/EXEC)
 Main PID: 7592 (code=exited, status=203/EXEC)

Aug 09 18:36:01 dwaves.org systemd[1]: Started onboot.
Aug 09 18:36:01 dwaves.org systemd[1]: Starting onboot...
Aug 09 18:36:01 dwaves.org systemd[1]: onboot.service: main process exited, code=exited, status=203/EXEC
Aug 09 18:36:01 dwaves.org systemd[1]: onboot.service: control process exited, code=exited status=203
Aug 09 18:36:01 dwaves.org systemd[1]: Unit onboot.service entered failed state.
Aug 09 18:36:01 dwaves.org systemd[1]: onboot.service failed.

# examine the problem
journalctl -xeu onboot.service

-- Unit onboot.service has begun starting up.
Aug 09 18:39:01 dwaves.org systemd[7617]: Failed at step EXEC spawning /scipt/start.sh: No such file or directory
-- Subject: Process /scipt/start.sh could not be executed
# fix error and try again
systemctl start onboot
# no output = good, no error
# get status, this is what we want
systemctl status onboot

# if you are happy with the result
# make it permanent
systemctl enable onboot
Created symlink from /etc/systemd/system/multi-user.target.wants/onboot.service
to
/etc/systemd/system/onboot.service.

# time to test a reboot
reboot
# after reboot your service should be running
docker ps -a; # lists all installed docker containers

# one also wants a graceful as possible shutdown/reboot
# instead of writing a service file i just use reboot script
vim /scripts/reboot.sh
# fill with
/scripts/stop.sh;
reboot;

# this at least is easy works reliable X-D
# but yes it is not very graceful if undocumented

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin