Bash – How to run Bash script on startup on Linux

bashstartup

I am running Linux 4.9.11-1.0.0 on my iMX7 dual development board. I have a test.sh file that I would like to run at startup. It currently only has a bunch of echo statements in it (just for testing purposes).

When I flick the switch on the board to turn it on, I see a bunch of initialization stuff come up. I would like the echo statements in my test.sh file to also be displayed in this initialization sequence on startup. Eventually once this is working, the plan is to do real stuff in place of the simple echo statements. I just want to make sure the script is running on startup at this point.

So far I have created the test.sh file and I am able to run it manually after the board has booted up. I have added the following to my /etc/rc.local file before the exit statement at the end:

sh '/home/root/test.sh'

I have verified that this rc.local file has #!/bin/sh -e at the top. I have also done:

$ chown root /etc/rc.local
$ chmod 777 /etc/rc.local

Once this was all done, I verified that it should be working properly by typing:

$ /etc/init.d/rc.local start

Everything is working as expected up until this point and I am seeing the echo statements when I do that last step. However, when I go and try to reboot the board, I do not see the echo statements at all in the printed initialization stuff.

What am I missing? Maybe it is actually running on startup but I just don't see the output for some reason?

Best Answer

If you've done everything such as chmod +x /etc/rc.local then your command is running, it's just not getting logged anywhere that you can see it.

Example

This is my /etc/rc.local:

$ cat /etc/rc.local
#!/bin/bash
touch /var/lock/subsys/local
echo "hickory stick"

Here I've used the modified Bash which logs to syslog which I discussed in this other U&L Q&A titled: Sending bash history to syslog.

When my system boots I see the following message getting logged via Bash:

/var/log/bash-log/127.0.0.1.log:2018-07-24T22:04:24.609094-04:00 centos7 rc.local: hickory stick

But no where else. To get this to log to syslog/rsyslog you typically can use the logger command to do so:

$ logger hi

You can then see it in your /var/log/messages or /var/log/syslog or journal -xef. Here I'm using journald:

$ journalctl -xef
Jul 24 20:23:04 centos7 bash[1629]: HISTORY: PID=1629 UID=0 USER=root CMD=man logger
Jul 24 20:23:24 centos7 bash[1629]: HISTORY: PID=1629 UID=0 USER=root CMD=logger hi
Jul 24 20:23:24 centos7 vagrant[1811]: hi

You should be able to use logger to capture output from commands in scripts like so:

$ cat /etc/rc.local
#!/bin/bash
touch /var/lock/subsys/local
echo "hickory stick" | logger

Now when we reboot:

$ journalctl -xef
...
Jul 24 20:31:41 centos7 bash[1629]: HISTORY: PID=1629 UID=0 USER=root CMD=journalctl -xe
Jul 24 22:24:00 centos7 logger[1286]: hickory stick
Jul 24 22:24:00 centos7 sshd[1270]: Server listening on 0.0.0.0 port 22.
Jul 24 22:24:00 centos7 sshd[1270]: Server listening on :: port 22.
Jul 24 22:24:00 centos7 systemd[1]: Starting Permit User Sessions...

We see our message there via logger that we added to /etc/rc.local.

Extra info

logger is a pretty useful and you can have it log to a file instead of via the -f switch, you can also control the tag that shows up in the logs with the -t switch.

$ logger -t "smurfs" hi
$ journalctl -xe | grep smurfs
Jul 24 20:38:24 centos7 smurfs[1764]: hi

References

Related Question