Monday, May 6, 2024
158
rated 0 times [  158] [ 0]  / answers: 1 / hits: 530452  / 2 Years ago, thu, june 16, 2022, 9:54:09

I have a script in a folder:



/path/to/my/script.sh


I need this script to run every time the system starts (even if no one logs in to the system). What do I need to do in order to make this happen?


More From » command-line

 Answers
5

You will need root privileges for any the following. To get root, open a terminal and run the command


sudo -i

and the command prompt will change to '#' indicating that the terminal session has root privileges.


Alternative #1: Add commands to /etc/rc.local


vi /etc/rc.local

with content like the following:


# This script is executed at the end of each multiuser runlevel
/path/to/my/script.sh || exit 1 # Added by me
exit 0

Alternative #2: Add an Upstart job (for systems older than 15.04) (not recommended)


Create /etc/init/myjob.conf


vi /etc/init/myjob.conf

with content like the following


description     "my job"
start on startup
task
exec /path/to/my/script.sh

Official statement from upstart website -> "Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd."


Alternative #3: Add an init script (obsolete)


Create a new script in /etc/init.d/myscript.


vi /etc/init.d/myscript

(Obviously it doesn't have to be called "myscript".) In this script, do whatever you want to do. Perhaps just run the script you mentioned.


#!/bin/sh
/path/to/my/script.sh

Make it executable.


chmod ugo+x /etc/init.d/myscript

Configure the init system to run this script at startup.


update-rc.d myscript defaults

[#33729] Saturday, June 18, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mouedi

Total Points: 420
Total Questions: 109
Total Answers: 116

Location: Philippines
Member since Wed, Aug 19, 2020
4 Years ago
mouedi questions
Mon, Mar 28, 22, 20:06, 2 Years ago
Sat, Dec 3, 22, 15:04, 1 Year ago
Thu, Mar 10, 22, 05:12, 2 Years ago
Wed, Jul 20, 22, 15:05, 2 Years ago
;