Running A Command At Startup

Home » CentOS » Running A Command At Startup
CentOS 10 Comments

On a support forum, I was told that to turn off my board’s blue led run:

echo none | sudo tee /sys/class/leds/blue\:heartbeat/trigger

Well, this does not survive a system reboot.  So I was told:

Add the off bit to

    /etc/rc.local

Add it above “exit 0”

So of course, CentOS is past using rc.local and recommends:

# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this fi

So can someone point me to how to make this into a simple systemd service?

thanks

10 thoughts on - Running A Command At Startup

  • Does your version of CentOS have the @reboot crontab option? If it does this is probably easier unless you want to learn how to write systemd files.

    Leroy Tennison Network Information/Cyber Security Specialist E: leroy@datavoiceint.com
    2220 Bush Dr McKinney, Texas
    75070
    http://www.datavoiceint.com TThis message has been sent on behalf of a company that is part of the Harris Operating Group of Constellation Software Inc. These companies are listed here
    . If you prefer not to be contacted by Harris Operating Group please notify us
    . This message is intended exclusively for the individual or entity to which it is addressed. This communication may contain information that is proprietary, privileged or confidential or otherwise legally exempt from disclosure. If you are not the named addressee, you are not authorized to read, print, retain, copy or disseminate this message or any part of it. If you have received this message in error, please notify the sender immediately by e-mail and delete all copies of the message.

  • if it’s CentOS/RHEL 7,  you can turn it into a service that starts after boot too,  and cintrol it with systemctl.

  • OK….

    I have had problems in the past with crontab parsing a command. Would I use:

    @reboot root echo none | tee /sys/class/leds/blue\:heartbeat/trigger

    ?

    Or do I have to make a script and run that?

    thanks

  • Since this is a crontab, you can use normal shell redirection:

    @reboot root echo none > /sys/class/leds/blue\:heartbeat/trigger

    in a file in /etc/cron.d/

    The ‘echo foo | sudo tee’ thing is what you do for people who are using sudo to echo output into a file — so often people think they can do ‘sudo echo none > /some/path’ and will be surprised it doesn’t work.

    I still think it makes sense to create it as a systemd unit.

  • Thanks.  This is my first encounter with ‘tee’.  I guess because I
    rarely use sudo, and work in su if I need to do root things.

    to learn enough about making systemd unit files.

    Thanks

  • But I think even the rc.local hack should work if you make it executable. At least that’s what the header of rc.local says.

    Regards, Simon

  • –I’d first create a utility script (untried code!) like this:

    /usr/local/sbin/BlueLedFunction.sh

    #!/bin/sh echo “$1” > /sys/class/leds/blue\:heartbeat/trigger

    Then I’d create /etc/systemd/system/BlueLedOff.service with appropriate sections to invoke that script with “none” as an argument and to run in your desired runlevel. (Take a look at the examples in
    /lib/systemd/system.) Then issue this to have it run at startup:

    systemctl enable BlueLedOff

    Note that custom unit files go in /etc/systemd/system to avoid having them overwritten by distro updates. You can customize existing unit files by either copying them from /lib/systemd to /etc/systemd or you can override single settings with specially-named subdirectories in /etc/systemd/system. See the unit file documentation for details.