-2

I want to write a bash script file (.sh) file in Ubuntu so that docker services start automatically on reboot.

Vendetta
  • 2,078
  • 3
  • 13
  • 31
megha rai
  • 29
  • 4

1 Answers1

0

User the cron service.

You need to use special string called @reboot. It will run once, at startup after Linux reboots. The syntax is as follows:

@reboot  /path/to/job
@reboot  /path/to/shell.script
@reboot  /path/to/command arg1 arg2
#So to run docker on reboot:
@reboot start docker

This is an easy way to give your users the ability to run a shell script or command at boot time without root access. First, run crontab command:

$ crontab -e

OR

# crontab -e -u UserName
# crontab -e -u vivek

So, to run a script called /home/vivek/bin/installnetkit.sh

@reboot /home/vivek/bin/installnetkit.sh
Vendetta
  • 2,078
  • 3
  • 13
  • 31
  • As per your example, in installnetkit.sh I call another scripts which start docker containers, those scripts need sudo permission. So how to provide sudo permission for execution of those scripts. – megha rai Nov 01 '19 at 14:18
  • You don't need to, it's just an example of what the command can do. – Vendetta Nov 01 '19 at 14:19
  • I don't believe docker requires super user to run, but if it does, just run crontab as root user: `sudo crontab -e` – Vendetta Nov 01 '19 at 14:20
  • @megharai more info on how to run cron processes as root can be found here: https://askubuntu.com/questions/948029/how-to-run-sudo-commands-in-cronjobs-in-ubuntu-16-04/948034 – Vendetta Nov 01 '19 at 14:23
  • 1
    If your problem is resolved, please accept an answer or close the thread. (You accept an answer by clicking the check mark under the votes for that answer, and you close the thread by clicking delete under your question) – Vendetta Nov 01 '19 at 14:54
  • Then try checking out this link https://docs.docker.com/config/containers/start-containers-automatically/ – Vendetta Nov 05 '19 at 15:05