I am trying to run a shell script on a Google Cloud Function.
I am using Node as the framework, and have tried different ways to run the script: execSync
, execFileSync
, spawnSync
, etc:
module.exports.main = function() {
try {
const output = require('child_process').execSync(__dirname + '/run.sh', [],
{
stdio: 'inherit',
shell: '/bin/bash'
});
}
catch (error) {
process.exit(error.status);
}
}
The behavior I get is very erratic. Overall I think it worked once, but the rest of the time I either get a connection error
and nothing happens, or the function terminates "successfully", but nothing from the shell script gets outputted, so I don't know whether it even ran or not.
To be more precise, the log lines I see when the function "fails" are only the following:
Function execution started
Function execution took 122 ms, finished with status: 'connection error'
I couldn't detect a pattern in when it fails and when it runs.
Exact reproduction steps:
- Create a Google Cloud Function
- set its runtime to Node 8
- set its trigger to Pub/Sub topic
- set its function to call to
main
- create a zip containing
index.js
with the content above (module.exports.main = ...
)run.sh
containing only#! /bin/bash
echo "hi"
- upload zip as source for Cloud Function
- go to Testing tab of Cloud function and hit "Test this functions"
- view logs