Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot make bash script work from cloud-init

Tags:

linux

bash

shell

Obviously I am doing something wrong here.

Cloud init script /etc/cloud/cloud.cfg

...
runcmd:
 - [ sh, /opt/cloud-init-scripts/whatever.sh ]

The script /opt/cloud-init-scripts/whatever.sh

#!/bin/bash
...
. /home/ubuntu/third-party/script.sh --silent

Third party script /home/ubuntu/third-party/script.sh

#!/usr/bin/env bash

function some_function() {
...

Error I am getting in /var/log/cloud-init-output.log

/opt/cloud-init-scripts/whatever.sh: 3: /home/ubuntu/third-party/script.sh: Syntax error: "(" unexpected

I must be missing something obvious here. I tried using source, . and sh when calling the third party script, tried changing shebangs everywhere but no success.

If I run the same command from command line it works.

like image 410
Tomas Avatar asked Mar 08 '23 00:03

Tomas


1 Answers

You have specified sh shell under runcmd, but have she-bang set to bash. The latter does not matter because if you run as sh /opt/cloud-init-scripts/whatever.sh it will be run with sh shell. I guess you are probably using a non POSIX shell feature which is incompatible with the sh shell.

Or alternatively if your intention is to run the script in bash shell, change the runCmd in cloud-init script to

runcmd:
 - [ bash, /opt/cloud-init-scripts/whatever.sh ]
like image 144
Inian Avatar answered Mar 14 '23 19:03

Inian