systemd action when a service fails? - systemd

Is there a standard way to get systemd to execute a command when a service fails? For example, send an email to somebody (or a passive alert to Nagios).
I realise I can write a wrapper around the ExecStart command, but I had hoped for something less ad-hoc.

OnFailure=
A space-separated list of one or more units that are activated when this unit enters the "failed" state. A service unit using Restart= enters the failed state only after the start limits are reached.
Source: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
One hacky way to test failures is to run the service with User=Dummy and then, while the service is running, destroy or modify the user Dummy using the command line.

Related

How to use cloud-init to setup a bash script to be triggered by systemd whenever finishes bootup and about to shutdown?

I'm targeting ubuntu 22.04
I know how to use cloud-init to do simple stuff like runcmd and add user using users in the yaml.
I have two scripts that I like to be triggered via systemd
before-shutdown-script.sh : this script is to be triggered before the server is shutdown or before the server goes into reboot. The script's job is to call a webhook so network must at least be working before the script is activated. The webhook is simply to tell a centralized place, hey this server is powering down.
upon-bootup-script.sh : this script is to be triggered when the server is booted up. The script's job is to call a webhook so network must at least be working before the script is activated. The webhook is simply to tell a centralized place, hey this server has just booted up.
I am not confident of how to use systemd and how to use cloud-init to configure this. How should I script the cloud-init yaml?

Laravel: How to detect if code is being executed from within a queued job, as opposed to manually run from the CLI

I found this similar question How to check If the current app process is running within a queue environment in Laravel
But actually this is the opposite of what I want. I want to be able to distinguish between code being executed manually from an artisan command launched on the CLI, and when a job is being run as a result of a POST trigger via a controller, or a scheduled run
Basically I want to distinguish between when a job is being run via the SYNC driver, manually triggered by the developer with eyes on the CLI output, and otherwise
app()->runningInConsole() returns true in both cases so it is not useful to me
Is there another way to detect this? For example is there a way to detect the currently used queue connection? Keeping in mind that it's possible to change the queue connection at runtime so just checking the value of the env file is not enough

Is it possible to trigger a script execution when stopping a windows service from services.msc?

I want to know if it is possible to configure a service to call a batch/powershell script when I stop it from services.msc.
While in Linux init.d services are fully programmable and even systemd services can have additional procedures I've yet to find a way to accomplish this on Windows.
Thanks in advance
You can configure services to run a program on failure, but if you are stopping the service via services.msc then that likely wouldn't count as a failure.
The only other option I can think of would be to set up a PowerShell script running as a scheduled task that either periodically checks the services running status, or (for a more foolproof option) looks at the event log for events indicating that the service has been stopped (since the last time the script checked) and then performs whatever actions you require.
Per the comment from montonero, you wouldn't need to run the scheduled task periodically as it could be configured to run when the event itself occurs. This is described here: https://blogs.technet.microsoft.com/wincat/2011/08/25/trigger-a-powershell-script-from-a-windows-event/
Use the Event Viewer “Attach Task to This Event…” feature to
create the task.
Launch "Event Viewer" and find the event. Once found, right-click on the event and select "Attach Task to
This Event...".

Restart Script from Beginning if Failures encountered in Jmeter

I have around 20 transactions in my jmeter script, I need to run a load test for 2000 Users.
I have added test checks and Assertions to validate if the transactions are passing
I want to introduce error handling , where the script start from the beginning if any of the assertions/transactions fail
I am not sure if I have understood your question correctly. If your aim is to stop the workflow for the current user and start from the beginning, then you could use until controller.
More information on this can be found here.

Bash script wouldn't start service

I'm trying to start a service via a script that I run through cron. Basically it just does this
/local/services/servicename status
/local/services/servicename stop
/local/services/servicename start
The service starts fine if I run the commands myself, but whenever I run it via the script, and I check for the service status manually, its response is always
Servicename Service is not running.
I am truly confuse right now. Any particular reason why a bash script wouldn't be able to start the services?
Not really an answer to your specific question, but definitely a useful tip for debugging cron behavior in general. Cron sends email messages to the user it runs as. If you run that in the root crontab, run the mail command in a terminal as root and you'll see the cron messages in your inbox. Check them by typing the message number (or man mail to learn how to use it).

Resources