1

Im looking for a way to run a bash script at boot that will simply display information. For example

#!/bin/bash
echo "test"
echo "info"
exit
matt
  • 23
  • 1
  • 1
  • 5

3 Answers3

2

If you put a script in /etc/profile.d/, it may run at startup, but only because root is active. If you do this, then if you run su, you may see that script start running again in your terminal. So, while this will run the script coincidentally at system start, it is not the system start that runs the script, but the activity of root that starts it.

If you want a script to run a true system startup, not just user login (including root login), then try a service. The advantage is that you can check its status with systemctl status ... Creating a service is not hard.

Tweak, then paste this into your terminal :

echo '[Unit]
Description=My amazing startup script

[Service] Type=simple ExecStart=/opt/path/to/myamazingstartup.sh

[Install] WantedBy=multi-user.target' > /etc/systemd/system/myamazingstartup.service

chmod 755 /etc/systemd/system/myamazingstartup.service systemctl enable myamazingstartup.service

Now, /opt/path/to/myamazingstartup.sh will run at every startup.

Optional to start now:

systemctl start myamazingstartup

Check how its running:

systemctl status myamazingstartup

  • The WantedBy=multi-user.target part is mandatory, otherwise the systemctl enable will fail. The alternative setting is graphical.target and others. You can read more here.
  • Type=simple is also mandatory
  • Put your own information in:
    • Description=
    • ExecStart=

Voilà! You have a service that runs your script at startup and even allows you to check its status.

Jesse
  • 165
2

Two ways to run a script at startup.

  • Add it to the /etc/.rc.local file
  • Add it to /etc/profile.d/foobar.sh

Though keep in mind, that the script won't print because there is no terminal for standard out.

An alternative might be to put your text in /etc/motd file. You will see the contents printed out every time you SSH in to the machine.

Giacomo1968
  • 55,001
spuder
  • 9,945
  • Out of curiosity, how is the text displayed at boot on the arch linux live cd used for installing arch? Because that is essentially what I am trying to emulate here. Is it with the /etc/motd method? – matt Mar 05 '15 at 06:04
  • Another option would be creating a crontab entry with @reboot as the "time". – Mario Mar 05 '15 at 08:03
  • Will the cron job run before the login screen? Also how would I get the text of the script cron runs to display on the screen? – matt Mar 06 '15 at 04:11
  • I have a problem with this, that it runs at login, not at boot. So, if I ssh into a server, it immediately runs that script at /etc/profile.d/foobar.sh in my terminal. Then, Ctrl + C leaves me in a -bash-5.1# terminal, not my normal terminal. This may only be for login or for root login, not for actual system startup. FYI – Jesse Aug 10 '22 at 06:16
-1

If you want your script to emit messages to the system log, use logger instead of echo.

#!/bin/bash
logger <<HERE
test
info
HERE
#exit  # Not really useful; the script will exit anyway
tripleee
  • 3,191