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
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
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
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 mandatoryDescription=ExecStart=Voilà! You have a service that runs your script at startup and even allows you to check its status.
Two ways to run a script at startup.
/etc/.rc.local file/etc/profile.d/foobar.shThough 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.
@reboot as the "time".
– Mario
Mar 05 '15 at 08:03
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
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