3

My Compute Engine vm when deployed run a startup script. Everything seems working well, but there is one command in the startup script which I think it doesn't.

I run the command

apt-get update && apt-get upgrade -y

This should install the newest versions of all packages (right?)

When I do this by hand it works, but it takes a lot of time. If I let the script do it I don't see any output when I connect over ssh so I have to asume it's still running. Is there a way I can see if it is still working and if it has finished or not?

This is the script:

#! /bin/bash
file="/var/www/check.txt"

if [ -e $file ]
then
apt-get update && apt-get upgrade -y
git -C /var/www/html pull https://xxxxxx:[email protected]/xxxxxx/xxxxx.git
else
apt-get update
apt-get install apache2 php libapache2-mod-php php-mcrypt php-mysql mysql-client -y
a2dismod autoindex
service apache2 restart

cat <<EOF > /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
        DirectoryIndex index.php index.cgi index.pl index.html index.xhtml index.htm
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
EOF

rm -rf /var/www/html
git clone https://xxxxxx:[email protected]/xxxxxx/xxxxx.git /var/www/html/

cat <<EOF > /etc/apache2/sites-available/xxxxx.conf
<VirtualHost *:80>
  ServerName  xxxxxx.com
  ServerAlias www.xxxxxx.com
  ServerAdmin [email protected]
  DocumentRoot /var/www/html/wwwroot
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
EOF

cat <<EOF > /etc/apache2/sites-available/020-xxxxx_xxxx.conf
<VirtualHost *:80>
  ServerName  xxxx.xxxxx.xxx
  ServerAlias xxxx
  ServerAdmin [email protected]
  DocumentRoot /var/www/html/xxxxx
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
EOF

cat <<EOF > /var/www/html/wwwroot/.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
EOF

sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf

wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
chmod +x cloud_sql_proxy
mkdir /cloudsql; sudo chmod 777 /cloudsql
./cloud_sql_proxy -dir=/cloudsql &

#rm /var/www/html/wwwroot/xxxxx/xxxxxx.php


#temporary for testing.
cat <<'EOF' > /var/www/html/wwwroot/includes/xxxxx.xxxx
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);




$username = "xxxxxx";
$password = "xxxxx";
$host = "/cloudsql/snappy-gantry-xxxxx:europe-west1:db1";
$dbname = "xxxxx";

  setlocale(LC_ALL, 'nld_nld');



$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
    $db = new PDO("mysql:unix_socket={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $ex) {
    die("Failed to connect to the database: " . $ex->getMessage());
}
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
EOF


a2dissite 000-default
a2ensite 010-xxxxx_main
a2ensite 020-xxxx_help
a2enmod rewrite
service apache2 restart
apt-get update && apt-get upgrade -y
sudo cat <<EOF > /var/www/check.txt
aanwezig!
EOF
fi
6
  • 2
    Please post the script where it doesn't work. Nov 22, 2017 at 9:12
  • The script does work, i'm just not sure if the line above works. all the other lines in my script i can verify. the line above is pasted from the script itself Nov 22, 2017 at 9:15
  • The line is correct, and it should always produce output. If you don't get output, you are doing something in your script that prevents it. Without seeing that script we can't debug it further. Nov 22, 2017 at 9:16
  • added the script. script is run from a google cloud bucket. presumably run from root account (?). i log in using my own account. Nov 22, 2017 at 9:24
  • i just checked my vm again. it does seem to be working fine. thing is i would like to know when the script is finished. now i had to wait for 20 minutes and just give the upgrade command again which now states all is up-2-date. this is ofcourse a mechanism to see if it works but i rather have a way to see the current output of the script or maybe a summary at the end. (Yes i know the script is rubbish but it works :-) ) Nov 22, 2017 at 9:29

4 Answers 4

3

Startup script output of Google Cloud Compute Engine instances is written to one of the following log files depending on the Linux distribution of the instance:

  • CentOS and RHEL: /var/log/messages
  • Debian: /var/log/daemon.log
  • Ubuntu 14.04, 16.04, and 16.10: /var/log/syslog
  • SLES 11 and 12: /var/log/messages

If for some reason you want to store updates of the script you may consider to redirect the output to a file and to upload id to Google Storage. For example:

  ...
$ command >> output
$ command >> output
$ gsutil cp output gs://yourbucketname/output
$ command >> output
  ...
$ command >> output
$ gsutil cp output gs://yourbucketname/output
$ ...

Note that you might consider to redirect as well the standard error with '2>>' to a file and upload it as well.

EDIT: I forgot to answer to one of your question. Yes you can check if the command is still running; since from the operating system point of view these commands are normal processes, therefore you will be able to check them running:

$ ps -aux

For example I got this output having a sleep as startup script

root       691  0.0  0.0   5840   696 ?        S    14:45   0:00 sleep 30
1

grep startup-scrip /var/log/syslog

4
  • 1
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – bjoster
    Jun 20, 2020 at 20:45
  • 1
    well running this command will print output of the startup script in the compute engine instance. I am looking at the question now "Is it possible to see output startup script in compute engine" mmm yeah, it is possible, run grep startup-scrip /var/log/syslog and you will see the output of startup script. I don't know what are you talking about.
    – yabloki
    Jun 21, 2020 at 10:31
  • 1
    This answer has no explanation for "what and why" happens here, regardless of the technical correctness (whis is the case IMHO). Try to copy the good parts of your comment into the answer to explain it.
    – bjoster
    Jun 22, 2020 at 8:49
  • It's a helpful command to see if even the script runs. I found it helpful.
    – Stryker
    Feb 12, 2021 at 18:57
0

For RHEL and CentOS you can also use journalctl command

$ journalctl

or if you want to follow the logs live as your startup script executes you, you can use the -f option:

$ journalctl -f
1
  • More specifically: journalctl -u google-startup-scripts -f
    – RCross
    Nov 19, 2021 at 11:41
0

You can use journalctl on Debian as of 2022 like this

sudo journalctl -u google-startup-scripts.service

see https://cloud.google.com/compute/docs/instances/startup-scripts/linux

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .