0

I'm trying to run a bash script from a website button on a raspberry pi, where the script is located in home/pi/. The code snippet below shows my current solution. Whenever the website button is pressed, it calls this block of code in a c file.

else if(strcmp(argv[1],"SelfTest") == 0)
    {
        char *command = "/bin/bash";
        char name[40];
        strcpy(name, "home/pi/SelfTest.sh");
        execl(command, command, name, NULL);
        return 0;
    }

I'm running this on a Raspberry Pi 3+, the script I'm running has 777 permissions and is owned by pi:www-data, if that helps.

I tested to see if the IF block actually got called by just having it create an empty directory called 'TEST'. The bottom snippet of code shows how I approached that. This seems to work without issue (as soon as I press the button on the website, I 'ls' home/pi/ and a new folder called 'TEST' is there). Unfortunately, when I run the top snippet of code(to run SelfTest.sh), the script doesn't run (the script itself is supposed to record the output to a log file with a timestamp, and pressing the button does not update the timestamp. The timestamp only updates when I run the script from within the terminal)

else if(strcmp(argv[1],"SelfTest") == 0)
    {
        char *command = "/bin/mkdir";
        char name[40];
        strcpy(name, "/home/pi/TEST");
        execl(command, command, name, NULL);
        return 0;
    }

What should be happening every time I press the website button is that it should run SelfTest.sh. I should be able to verify this by reading the logfile, but it never gets updated when I press the website button.

Please let me know if you need any extra information!

N. Sunilkumar
  • 65
  • 1
  • 7
  • Try redirecting stdout and stderr to a file and see if any error message is generated. – nimrodm May 29 '19 at 20:04
  • Why is there a `strcpy` at all? You can use `char const* name = "/home/pi/SelfTest.sh";`. – Ry- May 29 '19 at 20:05

2 Answers2

1

Unless if you are running this from /, this line is missing a slash before home:

strcpy(name, "home/pi/SelfTest.sh");

Also, execl will never return. You may want to look up information on the fork() and exec() pattern.

contrapants
  • 775
  • 4
  • 16
0

Apparently I'm super clumsy and didn't realize that I didn't need bash. I changed 'char *command = "/bin/bash";' to 'char *command = "/bin/sh";' and the script ran as expected!

N. Sunilkumar
  • 65
  • 1
  • 7