Run script with rc.local: script works, but not at boot

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

Thanks for contributing an answer to Stack Overflow!, Meta Stack Overflow , Stack Overflow Public questions & answers ,Stack Overflow em Português

In this example of a rc.local script I use io redirection at the very first line of execution to my own log file:

#!/bin/sh -e

#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0"
on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By
default this script does nothing.

exec 2 > /tmp/rc.local.log # send stderr from rc.local to a log file
exec 1 > & 2 # send stdout to the same log file
set - x # tell sh to display commands before execution

   /
   opt / stuff / somefancy.error.script.sh

exit 0
load more v
88%

Ask Ubuntu Meta ,Why is the command in rc.local not executed during startup?, Ask Ubuntu help chat ,I found in Ubuntu lxc containers that if rc.local has a perfectly correct shebang e.g.

Make sure the rc.local script is executable:

sudo chmod + x / etc / rc.local

Then, enable it:

sudo systemctl enable rc - local.service

Reboot the system or start the script manually by running:

sudo systemctl start rc - local.service

The service status can be displayed by running:

$ sudo systemctl status rc - local.service● rc - local.service - /etc/rc.local Compatibility
Loaded: loaded(/lib/systemd / system / rc - local.service; static; vendor preset: enabled)
Drop - In: /lib/systemd / system / rc - local.service.d└─ debian.conf
Active: active(running) since Mon 2018 - 04 - 02 10: 39: 44 - 03;
1 s ago
Process: 2044 ExecStart = /etc/rc.local start(code = exited, status = 0 / SUCCESS)
Main PID: 2049(svscanboot)
Tasks: 3
Memory: 556.0 K
CPU: 10 ms
CGroup: /system.slice/rc - local.service
load more v
72%

In this example of a rc.local script I use io redirection at the very first line of execution to my own log file:,The symbolic link from /etc/rc.local to the 'real' one in /etc/rc.d can get lost if one moves rc.local to a backup directory and copies it back or creates it from scratch, not realizing the original one in /etc was just a symbolic link.,Sounds like on dimadima's system, they are separate files, but /etc/rc.d/rc.local calls /etc/rc.local

In this example of a rc.local script I use io redirection at the very first line of execution to my own log file:

#!/bin/sh -e

#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0"
on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By
default this script does nothing.

exec 2 > /tmp/rc.local.log # send stderr from rc.local to a log file
exec 1 > & 2 # send stdout to the same log file
set - x # tell sh to display commands before execution

   /
   opt / stuff / somefancy.error.script.sh

exit 0
65%

Linux shell scripting tutorial, ➔ Linux shell scripting tutorial,Instead of shell script in /etc/rc.d/ or call /etc/rc.local we do this now. It only works on Linux and not other Unix variants. Then you just do:,How to set up Mariadb Galera cluster on Ubuntu or Debian Linux

#!/bin/sh

# add your commands
# call your scripts here

#
let us set stuff
for my wifi
   /
   sbin / iw phy0 wowlan enable magic - packet disconnect

# last line must be exit 0
exit 0
load more v
75%

I have a node.js script which need to start at boot and run under the www-data user. During development I always started the script with:,But there is no problem in writing a script and executing that: ,I ended up with upstart, which works fine.,But with instatiating a class, you can execute code in it, without using the wellknown (hope so) 'main' way:

I have a node.js script which need to start at boot and run under the www-data user. During development I always started the script with:

su www - data - c 'node /var/www/php-jobs/manager.js

Finally, my /etc/rc.local looks like this:

#!/bin/sh -e

#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0"
on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By
default this script does nothing.

su www - data - c 'node /var/www/php-jobs/manager.js >> /var/log/php-jobs.log 2>&1 &'

exit 0
load more v
40%

The Bash shell script we have created for this experiment runs once at startup and then exits. It does not remain in memory as a daemon because it was not designed to do so.,The service unit we will now create is a standard systemd service unit file. This simple file is used only to run the mystartup.sh script at startup.,In the following command, the -u option shows only entries for the mystartup unit:,All that is left is to enable the service so that it runs on startup:

Create the mystartup.sh file shown here and place it in /usr/local/bin (be sure to make it executable). Be sure to use the location for Bash that is correct for your distribution. For example, Debian-based distributions locate Bash at /bin/bash.

#!/usr/bin/bash

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# mystartup.sh
#
# This shell program is
for testing a startup like rc.local using systemd.
# By David Both
# Licensed under GPL V2
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# This program should be placed in /usr/local / bin

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This is a test entry

echo `date +%F" "%T`
"Startup worked" >> /root/mystartup.log
load more v
22%

First you need to create /etc/rc.local file, if it doesn't exists.,Make sure /etc/rc.local file is executable.,If you are running a Linux distribution that use Systemd, then you may find that your command in /etc/rc.local file would not run on system boot. The rc-local.service already exists in systemd and, if rc.local exists and is executable, it gets pulled automatically into multi-user.target.,You can check the status if you type the following command in terminal:

Contents of rc.local File

#!/bin/sh -e## rc.local## This script is executed at the end of each multiuser runlevel.# Make sure that the script will "exit 0" on success or any other# value on error.## In order to enable or disable this script just change the execution# bits.## By default this script does nothing.<Add terminal commands here without sude>exit 0

Other "undefined-undefined" queries related to "Run script with rc.local: script works, but not at boot"