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
: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:
But no where else. To get this to log to syslog/rsyslog you typically can use the
logger
command to do so:You can then see it in your
/var/log/messages
or/var/log/syslog
orjournal -xef
. Here I'm using journald:You should be able to use
logger
to capture output from commands in scripts like so:Now when we reboot:
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.References