0

I am using the Digital Ocean API to create a couple of Droplets(their name for a server).

These droplets can take user metadata which is basically a file with shell commands that I want to run to provision the server on creation.

An example is given here:

https://www.digitalocean.com/community/tutorials/an-introduction-to-droplet-metadata

To be blunt, I just need to check if the bash script successfully ran. I know a couple of cumbersome ways I might check this but I am hoping there would be something easier.

I guess the easiest would be to view the history of commands that ran on the machine or something like that. Is that possible for commands that ran from a shell script?

I know the history command, but that only works for commands entered while ssh'ed into the machine. This does not actually show commands from my bash script.

Hopefully somebody knows a quick and easy way to view all commands that ran on the machine.

Cheers.

Stephan-v
  • 103
  • 3
  • `I know a couple of cumbersome ways I might check this`. Ok, which? Why not use cron and check the cron.log? – 030 Apr 26 '17 at 06:55

1 Answers1

2

Your script is supposed to do something. Just check if that actually happened.

If that is somehow not possible, just add something like

touch /tmp/mycheckfile 

to the end of the script and then check if that file is present. Simple, easy and fast.

Note that this isn't proper error handling, all it does is to verify that the script was executed. If you put set -e at the top of the script, it will also terminate on any error and thus, don't create the file if anything did go wrong in the script.

If you want to get more sophisticated, you can use all the error reporting and output redirection methods bash allows, including logging to the system log with the logger command.


You can also output every line your script runs with set -x. In DO, the output will be listed in the syslog for the cloud-init service. You can also capture it in a file like so:

#!/bin/bash
set -x 
{
   do-something
   do-something-else
} 2>/tmp/mylogfile

with will result in a file content like this:

+ do-something
+ do-something-else

Note that I redirected the STDERR output with 2>, as this lines will be sent to that stream.

Sven
  • 98,649
  • 14
  • 180
  • 226