2

I have the following script:

#!/bin/bash
# wait, just in case hdd md0 in not mountet yet    
sleep 30 
# write Raid state in log-file
mdadm -D /dev/md0 > /home/main_usr/myScripts/raidHealth.log
#just check if it writes to the file
echo "just a Test" >> /home/main_usr/myScripts/raidHealth.log  

I´ve done the following things: 1. made file executable 2. changed owner to root 3. wrote script into crontab (sudo crontab -e | @reboot /home/main_usr/myScripts/checkRaid.sh)

So heres my Problem: When I run the script, everything works just perfect. But when I reboot my Computer the script runs, but my .log file just contains "just a Test". No output of the mdadm-command. I can´t explain why the mdadm-command is empty in this case. Maybe someone can help me out.

I also should mention that I need sudo to run the script (mdadm-command)

Mac Jo
  • 41
  • 6

3 Answers3

1

In your script you just need to add full path of mdadm which is /sbin/mdadm by default

Your script will be like:

#!/bin/bash
# wait, just in case hdd md0 in not mountet yet    
sleep 30 
# write Raid state in log-file
#-------------------just change this area-------------------------
/sbin/mdadm -D /dev/md0 > /home/main_usr/myScripts/raidHealth.log
#-----------------------------------------------------------------
#just check if it writes to the file
echo "just a Test" >> /home/main_usr/myScripts/raidHealth.log

and no need to change your script file location also using "crontab -e" is OK, just type crontab -e and paste following

@reboot /home/main_usr/myScripts/checkRaid.sh
scarface_90
  • 376
  • 2
  • 6
0

My guess is that mdadm program file is not found on crond's path.

Combining what others already suggested and adding excruciating detail:

  • ensure mdadm is on path
  • check and log $? from mdadm
  • run mdadm verbosely
  • timebox the mdadm invocation
  • check /var/log/*, $ man cron, $ man crontab, man7.org, etc.

so something like:

#!/bin/bash
logfile="/home/main_usr/myScripts/raidHealth.$$.log"
touch $logfile || (echo "could not touch ${logfile}" && exit 1)
echo "$(date) - BEGIN sleep" >> ${logfile}
# wait, just in case hdd md0 in not mountet yet    
sleep 30
echo "$(date) - END sleep." >> ${logfile}
which mdadm > /dev/null
rc=$?
if [ $rc -ne 0 ]
then
  echo "$(date) - FAIL - could not find cmd" >> ${logfile}
  exit $c
fi
echo "$(date) - BEGIN mdadm - will run $(which mdadm)" >> ${logfile}
# write Raid state in log-file
mdadm --verbose -D /dev/md0 > ${logfile} 2>&1
rc=$?
echo "$(date) - END mdadm - rc=[${rc}]" >> ${logfile}
exit $rc

caveats: no current access to gnu/linux system, meta-logging will muddy what you may have wanted to be simple mdadm output in your logfile.

0xb304
  • 93
  • 7
0

Thanks for your help.

The Problem was that the crontab -e section could´nt find the mdadm command (I thing it is a path issue).

So here´s how I fixed it in another way:

  • moved my script to /usr/local/sbin/checkRaid.sh
  • don´t used crontab -e but instead used /etc/crontab file and entered there

    */10 * * * * root /usr/local/sbin/checkRaid.sh

In normal words: every 10 Minutes run my script as root. It worked perfectly without any Path issues of the mdadm-command.

Mac Jo
  • 41
  • 6