Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Asked
Modified 1 month ago
Viewed 124 times
1

I am trying to make mdadm call into a simple bash script which writes a message in the kernel log in case of a state change.

I am running on a VM (qemu). The VM is running debian 12.5, and kernel version 6.1.118.

The script is as follows,

# cat /tmp/test.sh
#!/bin/bash

echo "raid array status change" > /dev/kmsg

I have added the following to the mdadm config file

PROGRAM /tmp/test.sh
ARRAY /dev/md0 UUID=<UUID of md0>

But when I do the test, it says the md0 is being picked up, and the correct program option is being used, but nothing gets printed in dmesg

mdadm --monitor --test /dev/md0 -1

Note that when I manually run the script, it prints the message in kernel log.

My questions

  1. Does the above process of calling the program depend on the mail setting also? Because my mail config is not set.
  2. Any idea what could be wrong or missing?
9
  • 2
    How exactly are you running the script manually (as /tmp/test.sh or as, say, bash /tmp/test.sh)? If the latter, is /tmp mounted noexec?
    steeldriver
    –  steeldriver
    2025-07-30 12:01:03 +00:00
    Commented Jul 30 at 12:01
  • I ran it as bash /tmp/test.sh. And I did try moving the script to /opt/ folder too, but there were no logs still.
    Haris
    –  Haris
    2025-07-30 12:16:50 +00:00
    Commented Jul 30 at 12:16
  • First of all, does running echo "raid array status change" > /dev/kmsg (as root) do what you want?
    Chris Davies
    –  Chris Davies
    2025-08-18 13:42:12 +00:00
    Commented Aug 18 at 13:42
  • Second, does running /tmp/test.sh (as root) - specifically not bash /tmp/test.sh - do what you want?
    Chris Davies
    –  Chris Davies
    2025-08-18 14:12:49 +00:00
    Commented Aug 18 at 14:12
  • 1
    @ChrisDavies Yes to both.
    Haris
    –  Haris
    2025-08-19 08:28:44 +00:00
    Commented Aug 19 at 8:28

1 Answer 1

0
+50

This is a suggestion to partition the problem. It isn't an answer as such but I can't fit all this into comments. I will delete it afterwards if I haven't turned it into a proper answer


First of all ensure that the functional components work as expected.

  1. Ensure that running echo "raid array status change" > /dev/kmsg as root does indeed generate a line that can be picked up by dmesg
  2. Ensure that running your code /tmp/test.sh as root - and specifically not bash /tmp/test.sh - does what you want

Assuming those tests succeed, try the following. Create the program /opt/event.sh as follows:

#!/bin/bash
#
event=$1
device=$2
shift 2
extras=("$@")

logger "MDADM EVENT TEST: event=$1, device=$2, extras=(${extras[*]})"
exit 0

and make it executable with chmod a+rx /opt/event.sh.

Edit the file /etc/mdadm/mdadm.conf and ensure there is exactly one PROGRAM line:

# program to run on an event
PROGRAM /opt/event.sh

Restart the MDADM monitoring service:

systemctl restart mdmonitor

Immediately look in the log files for the test event trigger that's generated when the monitoring service (re)starts:

find /var/log -type f -mmin -5 -print0 | xargs -0r grep -h MDADM

You are looking for a line like this:

2025-08-19T09:43:59.087253+00:00 rHost root: MDADM EVENT TEST: event=, device=, extras=()

If you find one the event.sh program has been correctly run. If you don't, it hasn't, and the problem is outside the mdadm subsystem.

3
  • Thanks for the response. I did not find the above log.
    Haris
    –  Haris
    2025-08-19 12:23:09 +00:00
    Commented Aug 19 at 12:23
  • @Haris if you run logger THIS IS A TEST you should see it in /var/log/user.log
    Chris Davies
    –  Chris Davies
    2025-08-26 12:32:30 +00:00
    Commented Aug 26 at 12:32
  • 1
    Hi Chris. I am now able to run the script and log to kernel log. I am not sure what was wrong, but it suddenly started working one morning. Thanks for the help.
    Haris
    –  Haris
    2025-08-27 08:36:00 +00:00
    Commented Aug 27 at 8:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.