I have created a script to do some stuff.
Running the script manually (as root) works just fine. I can see new timestamps on the files that the script handles.
I’ve set up a Cron Job to run this script. I don’t get any errors anywhere when the job is triggered, and triggering it manually (“run job” button) says the job was completed succesfully… but the script doesn’t actually run as the timestamps on the files that the script handles are not changed.
The job is set up to run as root
. The command is correct as I can copy the command and run it in a shell and see the timestamps being updated.
But it never works when run from “Cron Jobs”, neither manually nor triggered.
How can I get this to work?
EDIT:
When I run the script directly I can see it in ps aux | grep myscript
. When it is triggered or run through “run job” it never shows up in ps aux
EDIT 2:
Preceeding the cron command with /bin/bash
, as suggested elsewhere, had no effect. (i.e. /bin/bash /mnt/mypool/myscript.sh
)
Please provide more details, specifically which version of TrueNAS are you running. Running a script is different in SCALE these days.
But if you are running SCALE 24.04 or later…
cd /mnt/mypool && ./myscript.sh
Let me know how this works out. If it fails, you may need to post your script.
@joeschmuck This is on SCALE 25.04.0.
I tried a script that only did a sleep 30
(to simulate something that takes a while), and this script worked (I could see it in ps aux
).
I then started adding stuff from my other script into this test script, and the error has something to do with the checks I do to see if the script is already running, so that it doesn’t start two instances at once:
pseudopid="`pgrep -f $0 -l`"
actualpid="$(echo "$pseudopid" | grep -v 'sudo' | awk -F ' ' '{print $1}')"
if [[ `echo $actualpid` != "$$" ]]; then
echo "Another instance of this script is already running!" >&2
exit 1
fi
I found this on StackOverflow, and it works perfectly fine when running the script manually, but Cron Job seems to have a problem with this. Why, though, is beyond me.
EDIT:
When it does run, it creates three processes. I would expect only one:
root 1645833 0.0 0.0 8028 3152 ? S 16:51 0:00 sudo -H -u root sh -c /mnt/mypool/myjob.sh > /dev/null
root 1645834 0.0 0.0 2576 1200 ? S 16:51 0:00 sh -c /mnt/mypool/myjob.sh > /dev/null
root 1645835 0.0 0.0 4344 2416 ? S 16:51 0:00 /bin/bash /mnt/mypool/myjob.sh
EDIT 2:
After more testing it seems that it always enters that if
, even when it is not already running. I noticed some notifications in the topright corner of TrueNAS which took me to a “Jobs” page that showed me errors, and they all said “Another instance of this script is already running!”.
So something is most definitely wrong with the initial check in the script. I still don’t know why, though… It doesn’t seem to be related to the three processes as the errors occurs only once… and if there was actually three processes then one of them should have worked and two should have failed.
If this is the entire script, you are missing something… you need the first line.
#!/bin/bash
pseudopid="`pgrep -f $0 -l`"
actualpid="$(echo "$pseudopid" | grep -v 'sudo' | awk -F ' ' '{print $1}')"
if [[ `echo $actualpid` != "$$" ]]; then
echo "Another instance of this script is already running!" >&2
exit 1
fi
I too had issues with multiple instances running and I resolved it by creating a lock directory in /tmp. See the code below. You will need it all and change the pieces you want. it will toss you an error message and exit if you have an instance already running.
if ! mkdir /tmp/multi_report.lock; then
printf "Script is already Running... Exiting\n" >&2
printf "If this message is in error, remove '/tmp/multi_report.lock' directory or just reboot TrueNAS to recover.\n" >&2
exit 1
fi
fi
trap 'rm -rf /tmp/multi_report.lock' EXIT # Remove the lock directory on exit
Hope this helps.
EDIT: I did not run your script or proof it. Trying to use PIDs sucks.
1 Like
I did have the shebang at the top of the script on my end. I just abbreviated it a bit too much here 
I also found several other StackOverflow posts that used directories and files as locks, but I liked the one I ended up using because it didn’t create anything that needed to be cleaned up, though it seems I have to go that route.
Thanks for the lock script. I’ll give it a try 
@joeschmuck Tried with your lock and now everything works as expected (though you pasted one fi
too many
). Thanks for the help 
Sorry about the extra fi
. It was a terrible copy/paste operation.
If you have further questions, feel free to ask but Google it likely your best location for BASH help. BASH just sucks but it is what we have.