Problem with executing .sh file

I’m having masses of problems with this for some reason and have tried so many variations too but they all seem to fail with some error (126, 127, exit code 2, patch doesn’t exist).

copy /var/opt/BESClient/__BESData/CustomSite_UnixCustomContent/__Download/AV_EDR-crontab-Add_rhel7.sh /var/tmp/AV_EDR-crontab-Add_rhel7.sh
wait chmod 755 /var/tmp/AV_EDR-crontab-Add_rhel7.sh
wait sh -c "/var/tmp/AV_EDR-crontab-Add_rhel7.sh"
delete /var/tmp/AV_EDR-crontab-Add_rhel7.sh

I know there are multiple ways to do this icluding writing the .sh file directly into the fixlet using appendfile however when I do that it see’s part of the .sh file as relevance and gives a relevance expression error.

The error I get specifically with the above code though is (child exec () failed (errno is 2: No such file or directory) wait sh -c “/var/tmp/AV_EDR-crontab-Add_rhel7.sh” however the folder and file do exist and all of the other parts complete with success.

Where is it all going wrong (bonus points for not laughing at me if it’s obvious).

Given the error message, I would summarize that the copy failed, but didn’t error out until you attempted to reference the SH script in following commands. Here’s how I’d adjust the Action Script:

// Good housekeeping
delete /var/tmp/AV_EDR-crontab-Add_rhel7.sh

// Make sure that tmp folder exists
if (not exists folder "/var/tmp")
	wait mkdir -p "/var/tmp"
endif

// Move file to tmp and setup permissions
move __Download/AV_EDR-crontab-Add_rhel7.sh /var/tmp/AV_EDR-crontab-Add_rhel7.sh
wait chmod 755 /var/tmp/AV_EDR-crontab-Add_rhel7.sh
parameter "__ExitCode01" = "{if exist exit code of action then exit code of action as string else "999"}"
if {parameter "__ExitCode01" != "0"}
	exit {parameter "__ExitCode01"}
endif

// Execute AV-EDR script
wait sudo su - root -c "cd /var/tmp; ./AV_EDR-crontab-Add_rhel7.sh"
parameter "__ExitCode02" = "{if exist exit code of action then exit code of action as string else "998"}"
if {parameter "__ExitCode02" != "0"}
	exit {parameter "__ExitCode02"}
endif

// Good housekeeping
delete /var/tmp/AV_EDR-crontab-Add_rhel7.sh

Then just change the success criteria of your fixlet as follows:

image

I hope this helps.

2 Likes

It looks to be moving further forward but I keep getting an exit code of 126 on execution now.

In cases like this, I implement redirection of standard error and output to an execution log file in order to identify which section of my script is throwing the undesirable exit code. Additionally, you could add some internal error handlers and logging to your script in order to accomplish similar collection/detection.

However, error code 126 typically imply that you do not have permission to execute the script. Try the following change to the Action Script.

wait sudo su - root -c "cd /var/tmp; chown root AV_EDR-crontab-Add_rhel7.sh; chmod 775 AV_EDR-crontab-Add_rhel7.sh"

I don’t know that the above will fix the exit 126 code, but it’s worth a try.

Ah sadly that didn’t fix the problem either :frowning:

what’s strange though is that a user can execute that script from the copy location and it doesn’t give any error at all :frowning: