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:xxxxxxxx@bitbucket.org/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:xxxxxx@bitbucket.org/xxxxxx/xxxxx.git /var/www/html/

cat <<EOF > /etc/apache2/sites-available/xxxxx.conf
<VirtualHost *:80>
  ServerName  xxxxxx.com
  ServerAlias www.xxxxxx.com
  ServerAdmin webmaster@xxxxx.xx
  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 webmaster@xxxxx.xx
  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. – Gerald Schneider Nov 22 '17 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 – Lennart Giaccotto Nov 22 '17 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. – Gerald Schneider Nov 22 '17 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. – Lennart Giaccotto Nov 22 '17 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 :-) ) – Lennart Giaccotto Nov 22 '17 at 9:29
2

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 '20 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 '20 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 '20 at 8:49
  • It's a helpful command to see if even the script runs. I found it helpful. – Stryker Feb 12 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.