Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Startup script doesn't seem to work

I've recently started using Google's Compute engine for some of my projects the problem is my startup script doesn't seem to work, For some reason my script just doesn't work, the VM has the startup-script metadata and it works fine when I run it manually with:

sudo google_metadata_script_runner --script-type startup

Here is what I am trying to run on startup:

#!/bin/bash
sudo apt-get update
sudo rm -f Eve.jar
sudo rm -f GameServerStatus.jar
wget <URL>/Eve.jar
wget <URL>/GameServerStatus.jar
sudo chmod 7777 Eve.jar
sudo chmod 7777 GameServerStatus.jar
screen -dmS Eve sh Eve.sh
screen -dmS PWISS sh GameServerStatus.sh

There are no errors in the log either, it just seems to stop at the chmod or screen commands, Any ideas?

Thanks!

like image 693
Brian Walker Avatar asked May 20 '17 10:05

Brian Walker


People also ask

How do I get a script to run on startup?

On Windows, the simplest way of running a program at startup is to place an executable file in the Startup folder. All the programs that are in this folder will be executed automatically when the computer opens. You can open this folder more easily by pressing WINDOWS KEY + R and then copying this text shell:startup .

Where are Windows startup scripts stored?

The default location for local logon scripts is the Systemroot\System32\Repl\Imports\Scripts folder.

What user do startup scripts run as?

The computer startup and shutdown scripts execute under the local system account; user logon and logoff scripts run as the current user account.


2 Answers

To add to kangbu's answer:

Checking the logs in container-optimized OS by

sudo journalctl -u google-startup-scripts.service

showed that the script could not find the user. After a long time of debugging I finally added a delay before the sudo and now it works. Seems the user is not registered when the script runs.

#! /bin/bash

sleep 10  # wait...
cut -d: -f1 /etc/passwd > /home/user/users.txt  # make sure the user exists
cd /home/user/project  # cd does not work after sudo, do it before
sudo -u user bash -c '\
source /home/user/.bashrc && \
<your-task> && \
date > /home/user/startup.log'
like image 188
yspreen Avatar answered Sep 20 '22 01:09

yspreen


I have the same problem @Brina mentioned. I set up metadata key startup-script and value like:

touch a
ls -al > test.txt

When I ran the script above sudo google_metadata_script_runner --script-type startup, it worked perfectly, However if I reset my VM instance the startup script didn't work. So, I checked startup script logs

...
Jul  3 04:30:37 kbot-6 ntpd[1514]: Listen normally on 5 eth0 fe80::4001:aff:fe8c:7 UDP 123
Jul  3 04:30:37 kbot-6 ntpd[1514]: peers refreshed
Jul  3 04:30:37 kbot-6 ntpd[1514]: Listening on routing socket on fd #22 for interface updates
Jul  3 04:30:38 kbot-6 startup-script: INFO Starting startup scripts.
Jul  3 04:30:38 kbot-6 startup-script: INFO Found startup-script in metadata.
Jul  3 04:30:38 kbot-6 startup-script: INFO startup-script: Return code 0.
Jul  3 04:30:38 kbot-6 startup-script: INFO Finished running startup scripts.

Yes. they found startup-script and ran it. I guessed it had executed as an another user. I changed my script like this:

pwd > /tmp/pwd.txt
whoami > /tmp/whoami.txt

The result is:

myuserid@kbot-6:/tmp$ cat pwd.txt whoami.txt
/
root

Yes. It was executed at the / diectory as root user. Finally, I changed my script to sudo -u myuserid bash -c ... which run it by specified userid.

like image 30
kangbu Avatar answered Sep 22 '22 01:09

kangbu