18

I put one script in /etc/init.d/abc.sh. Now I want it to run at startup.

How can I do that with chkconfig?

chkconfig --add abc.sh

But I am not sure how it works.

1

5 Answers 5

19

Scripts which run on startup are not said to be a script — if you want the script to be executed at startup, change the following in /etc/crontab:

@reboot /path/to/script
4
  • 1
    Would it run as the root user if placed in the cron? Dec 29, 2015 at 14:33
  • 3
    That's a brilliant idea! It is portable & simple. It does not harm the boot. It comes after system modules startup with accurate user permissions.... In my case on centos 7, it would be "@reboot root /path/to/script" Jan 24, 2016 at 11:47
  • 1
    ERROR (getpwnam() failed): add the user running the script. E.g. @reboot root /path/to/script
    – lainatnavi
    Jul 13, 2023 at 8:30
  • Hi. Would it be executed after a shutdown also?
    – elysch
    Nov 5, 2023 at 1:11
10

I often just use

/etc/rc.local

So, like...

echo "/etc/init.d/abc.sh" >> /etc/rc.local

should hack it for ya. There are probably more proper places, but whatever, it works, on most distros it seems.

4
  • that script is a daemon and i have abc start stop restart configurd
    – Mirage
    Jul 12, 2011 at 20:42
  • Perhaps you want to call it by entering: 'service abc start' Nov 9, 2015 at 17:21
  • /etc/rc.local contains the following warning message: ``` # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. ``` I think there must be a better place to run abc.sh.
    – Brian
    May 31, 2021 at 3:27
  • 1
    Yeah, after a decade since posting my response, the conventions have shifted. Jul 13, 2021 at 15:05
2

Have a look at other, existing init-scripts. In every proper rh-style init-script there are three hints: the runlevels, where the script should be started and the priority at which it should be started/stopped. The sum of start+stop should be normally 100.

This comment-line is evaluated by chkconfig.

1

You can't unless you read /usr/share/doc/initscripts-*/sysvinitfiles and structure your script accordingly.

1

You are on the right track. Your bash script is in the right location and you have added it to your chkconfig, which means your script is installed.

Please note that /etc/init.d is a symbolic link to /etc/rc.d/init.d

After adding your script, you need to select which runlevel you want to activate it on: chkconfig --level 35 abc on will activate your script on runlevels 3 and 5, your most common startups.

Please consult chkconfig --help for more info.

PS. you can also use ntsysv which is a tui for chkconfig.

You must log in to answer this question.

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