Is it possible to see output startup script in compute engine

6,294

Solution 1

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

Solution 2

grep startup-scrip /var/log/syslog

Share:
6,294

Related videos on Youtube

Lennart Giaccotto
Author by

Lennart Giaccotto

Updated on September 18, 2022

Comments

  • Lennart Giaccotto
    Lennart Giaccotto almost 2 years

    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
    
    • dortegaoh
      dortegaoh over 6 years
      Please post the script where it doesn't work.
    • Lennart Giaccotto
      Lennart Giaccotto over 6 years
      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
    • dortegaoh
      dortegaoh over 6 years
      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.
    • Lennart Giaccotto
      Lennart Giaccotto over 6 years
      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
      Lennart Giaccotto over 6 years
      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 :-) )
    • dortegaoh
      dortegaoh over 6 years
      I don't see anything wrong with your script. But I'm not familiar with the google compute engine. If they suppress the output (I assume you are looking on some kind of local console) you can send yourself an email at the end of the script run, or trigger some webhook with wget or curl. Apart from that, I don't think there is anything you can do.
  • bjoster
    bjoster about 4 years
    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
  • yabloki
    yabloki about 4 years
    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.
  • bjoster
    bjoster about 4 years
    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.
  • Stryker
    Stryker over 3 years
    It's a helpful command to see if even the script runs. I found it helpful.
  • RCross
    RCross over 2 years
    More specifically: journalctl -u google-startup-scripts -f