Go Down

Topic: [Solved] Process begin syntax? Requires #!/bin/ash in scripts (Read 886 times) previous topic - next topic

abuster

Oct 09, 2014, 03:27 pm Last Edit: Oct 10, 2014, 10:57 am by abuster Reason: 1
Hi all :-)

I've upgraded to 1.5.0, and the process syntax below stopped working:
Code: [Select]
Process p;
p.begin("/path/to/shell_script.sh");
p.run();


Though, this syntax works:
Code: [Select]
Process p;
p.runShellCommand("/path/to/shell_script.sh");


Is this expected?

Here is a debug script I made:
Code: [Select]
#include <Bridge.h>
#include <Console.h>
#include <Process.h>

void setup() {
 Bridge.begin();
 Console.begin();
}

void loop() {
 Process p;
 p.runShellCommand("/root/ok.sh");
 //p.begin("/root/ok.sh");
 //p.run();
 int i = 0;
 char response[256];
 response[0] = '\0'; // empty as default
 Console.print("Response size: ");
 Console.println(p.available());
 while (p.available() > 0 && i < 256) {
   response[i] = p.read();
   i++;
   response[i] = '\0';
 }
 Console.print("Response: '");
 Console.print(response);
 Console.println("'");
 Console.print("Exit code: ");
 Console.println(p.exitValue());
 delay(1000);
}


Using p.begin and p.run gives:
Code: [Select]
Response size: 0
Response: ''
Exit code: 0


Using p.runShellCommand gives:
Code: [Select]
Response size: 7
Response: 'jej
OK
'
Exit code: 1


From console/ssh:
Code: [Select]
root@Arduino:~# cat ok.sh
echo "jej"
/root/ok.py
exit 1
root@Arduino:~# cat ok.py
#!/usr/bin/env python
print("OK")
root@Arduino:~# ls -l ok.*
-rwxr-xr-x    1 root     root            34 Oct  9 14:39 ok.py
-rwxr-xr-x    1 root     root            30 Oct  9 15:15 ok.sh
root@Arduino:~# ./ok.sh
jej
OK
root@Arduino:~# echo $?
1

abuster

Peeking inside Process.cpp one helped me find the difference between runShellCommand and begin:

Code: [Select]
unsigned int Process::runShellCommand(const String &command) {
  runShellCommandAsynchronously(command);
  while (running())
    delay(100);
  return exitValue();
}

void Process::runShellCommandAsynchronously(const String &command) {
  begin("/bin/ash");
  addParameter("-c");
  addParameter(command);
  runAsynchronously();
}


In other words, one can do:
Code: [Select]

Process p;
p.begin("/bin/ash");
p.addParameter("-c");
p.addParameter("/path/to/script.sh");
p.addParameter("param1");
p.run();


But the real error was in ok.sh, adding #!/bin/ash helps. When logged in, ash already runs.

Code: [Select]

root@Arduino:~# cat ok.sh
#!/bin/ash
echo "OK"

federicofissore

Indeed an executable script requires the starting "hashbang". Can we consider this solved then? If so, can you change the title of the topic?

Go Up