Jiby_1 0 Light Poster

I have wriiten a script to switch on buzzer and given the path of that script in '/etc/rc.local'. So after booting my script works and it make the buzzer to switch on...

my question is

i want to switch the buzzer off when my device is shutdown.(for startup we use rc.local file is there any file to edit and use while shutdown.)

Dani AI

Generated

— Short, reliable approaches for shutting the buzzer off at shutdown.

A recommended, robust solution is to manage the buzzer with a dedicated systemd unit that runs the “on” action at boot and the “off” action at service stop (which systemd runs during shutdown). systemd supports ExecStop= for exactly this use; it is the preferred way to run cleanup/teardown commands when a unit is stopped. (See the ExecStop semantics in the systemd service documentation.) systemd.service

Example unit (place as /etc/systemd/system/buzzer.service), make sure the start/stop scripts are executable and use absolute paths:

[Unit]
Description=Buzzer control

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/buzzer-on.sh
ExecStop=/usr/local/bin/buzzer-off.sh

[Install]
WantedBy=multi-user.target

Reload and enable the unit so it is active across boots: systemctl daemon-reload and systemctl enable --now buzzer.service. Note: ExecStop= is only executed for a unit that was actually started successfully, so if the buzzer is started elsewhere (for example only from /etc/rc.local) the unit’s ExecStop will not be invoked — migrate the start into the unit or ensure the service is the authoritative manager. /etc/rc.local is provided primarily for compatibility; prefer dedicated units for predictable shutdown behavior. systemd-rc-local-generator

As a quicker but less flexible fallback, a simple executable script placed in /lib/systemd/system-shutdown/ (or /usr/lib/systemd/system-shutdown/) will be run at the very end of the shutdown sequence with an argument like poweroff/reboot; this can call your buzzer-off script. Be cautious: those shutdown scripts run in a minimal environment and in parallel, so hardware access or complex commands may fail. systemd-shutdown

Troubleshooting notes: use journalctl -u buzzer.service to inspect ExecStart/ExecStop logs, check executable bits (chmod +x), use absolute paths in scripts, and remove the old rc.local entry once the service manages the buzzer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.