I have a (.sh) script that:
- Creates a file
- Reads a(ll) XML file
- Writes specific info to a .txt file
When I execute it manually using the ./mappingToText.sh command, a file called autorisaties.txt is created. This file has some of the contents from the xml files.
When I run this same script from the crontab, the file is created, but it is empty (0 bytes). I have found a whole lot of different topics on this issue, and tried a few solutions:
- I tried using >(>) and 2>&1 to write directly to the file
- Applied some changes to the code that suppose to work (based on other topics)
- Using full paths in the script
- Using this piece of code in the crontab: SHELL=/bin/bash
None have yet solved the problem for me.
Cronjob:
35 11 * * * /opt/IBM/taddm/dist/var/policy/ibmsecauthz/policy/rolemapping/AuthorizationManagerPolicyContextId_role/mappingToText.sh
Script:
#!/bin/bash
#Filepath van logfile
LOGFILE=/opt/IBM/taddm/dist/var/policy/ibmsecauthz/policy/rolemapping/AuthorizationManagerPolicyContextId_role/autorisaties.txt
#Kijken of logfile bestaat
if test -f "$LOGFILE"; then
#log bestaat dus verwijder deze
rm $LOGFILE
fi
#log bestaat niet dus maak deze aan
touch $LOGFILE
#geef file rechten (i have been trying/playing around using different users, will prob not use this later)
chmod 777 $LOGFILE #Issue also occurs when not using chmod
#Doorloop alle XML bestanden
for FILE in *;
do
#Als het bestand "principal" in de naam heeft
#if [ $FILE == *"principal"* ]; then
if echo $FILE | grep -q "principal"; then
#Naam (String) van de user ophalen uit het XML bestand
name=$(xmlstarlet sel -T -t -m "//*[local-name()='Subject']/*[local-name()='SubjectMatch']/*[local-name()='AttributeValue']/*[local-name()='DataValue']/*[local-name()='SimpleValue']" -v . $FILE)
#Rollen (Array) ophalen uit het XML bestand
roles=$(xmlstarlet sel -T -t -m "//*[local-name()='Condition']/*[local-name()='Apply']/*[local-name()='AttributeValue']/*[local-name()='DataValue']/*[local-name()='SimpleValue']" -v . -n $FILE)
#Code snippet: sorteren van de array
IFS=$'\n' sortedRoles=($(sort <<<"${roles[*]}"))
unset IFS
#Helper variable
previousRole=""
#Doorlopen van de rollen
for i in "${sortedRoles[@]}"
do
#Omdat rollen soms dubbel in de XML staan
if [ "$i" != "$previousRole" ] ; then
#Timestamp voor log ophalen
date=$(date %Y-%m-%d_%H:%M:%S)
#Underscore (_) vervangen door spatie
adjustedDate=${date//[_]/ }
#Schrijf autorisaties weg naar logfile
echo $adjustedDate " - Naam van gebruiker: " $name " - Rol van gebruiker: " $i >> $LOGFILE
fi
previousRole=$i
done
fi
done
How do I make this script work with a cronjob? Thanks
EDIT Triplee mentioned CronJob not running
Following his steps:
- Is the Cron daemon running? yes
- Is cron working? yes
- Is the command working standalone? yes
- Can cron run your job? Yes, no errors are found in the cron log
- Check permissions Permissions are fine. Can also write to directory
- Check paths I am using actual paths, that should be enough right?
- Don't suppress output while debugging Tried >>cron.out 2>&1 , see below
crontab -l:
48 12 * * * /opt/IBM/taddm/dist/var/policy/ibmsecauthz/policy/rolemapping/AuthorizationManagerPolicyContextId_role/mappingToText.sh >>/opt/IBM/taddm/dist/var/policy/ibmsecauthz/policy/rolemapping/AuthorizationManagerPolicyContextId_role/cron.out 2>&1
ll:
-rw-r--r--. 1 root root 0 Jan 13 12:48 autorisaties.txt
-rw-r--r--. 1 root root 0 Jan 13 12:48 cron.out
CodePudding user response:
You're using for FILE in *
without changing the working directory before.
Looks like the script is run from a different working directory by cron.
Have you tried for FILE in /path/to/xml/files/*
? Even though you write "I'm using actual paths, right?": Your code does not reflect that at this point.