4

This is my first time with upstart and am using EC2.

os/upstart = aws base AMI latest version - upstart-0.6.5-13.3.13.amzn1.x86_64

I can not get the following script to launch when waiting for the network to initiate. It will work if using standard

 start on runlevel [2345]
 stop on runlevel [!2345]

I have also tried various combinations of

start on filesystem and net-device-up IFACE=eth0
start on filesystem and net-device-up IFACE!=lo

Any suggestions would be grateful

description "test for on network start"


start on (started network-interface
      or started network-manager
      or started networking)

stop on (stopping network-interface
     or stopping network-manager
     or stopping networking)


script
    touch /home/ec2-user/myFile.txt
end script 
art vanderlay
  • 171
  • 1
  • 3
  • /var/log/upstart/ should contain a log file with your service's output. Perhaps it will provide more direction? – Andrew Jun 20 '14 at 20:50
  • I did see that mentioned a few times, but no such folder/file for the .conf exists. Not sure if this is an EC2 thing but I could not get it to log at all – art vanderlay Jun 20 '14 at 20:52

2 Answers2

2

Upstart is an event based system. For your job to execute at a correct stage it may need to wait for particular event(s) emitted by other start up job(s).

Possible solutions will differ from system to system and will depend on how upstart jobs are defined and what events they emit. It may also depend on the upstart version installed on the system.

Please note that upstart might not write it's logs to /var/log/upstart. Earlier versions may use syslogd while newer ones are more likely to write logs to /var/log/upstart.

Below solution will only work on the type of the system mentioned in the question:

os/upstart = aws base AMI latest version - upstart-0.6.5-13.3.13.amzn1.x86_64

On this system upstart didn't log job echo statements anywhere. So this part may require extra configuration.

Job definition:

description "testjob"

start on started elastic-network-interfaces

script
        echo $(ping -c 1 serverfault.com) > /var/log/testjob.log
end script

Test on a real EC2 instance:

# cat /var/log/testjob.log 
PING serverfault.com (198.252.206.140) 56(84) bytes of data. 64 bytes from stackoverflow.com (198.252.206.140): icmp_seq=1 ttl=51 time=72.9 ms --- serverfault.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 73ms rtt min/avg/max/mdev = 72.950/72.950/72.950/0.000 ms
grekasius
  • 2,046
  • 11
  • 15
  • unfortunately this did not trigger the script, any other suggestions welcome. – art vanderlay Jun 23 '14 at 13:18
  • Does your script work as expected if you run it manually? – grekasius Jun 23 '14 at 13:27
  • yes, script works as expected. using start on runlevel [2345] it fires, just too early – art vanderlay Jun 23 '14 at 19:18
  • What Ubuntu version you are running and what version of Upstart you have installed? – grekasius Jun 23 '14 at 19:44
  • upstart using 'stopped networking' is so borked for this. Totally counter-intuitive. `# service networking status` => `networking start/running` ... if it's in `start/running' state how can it be in stopped state? – Brian Onn Jun 23 '14 at 20:26
  • After doing some tests it turned out that `start on stopped networking` and `start on static-network-up` don't work well if interface gets it's IP configuration from DHCP. I updated my answer based on test results. – grekasius Jun 24 '14 at 08:01
  • os/upstart = aws base AMI latest version - upstart-0.6.5-13.3.13.amzn1.x86_64 – art vanderlay Jun 24 '14 at 14:07
  • Please state this information in your question because it's pretty significant. – grekasius Jun 24 '14 at 14:53
  • @Giedrius Rekasius, tried the new script and I notice two things. 1) by default, no folder /var/log/upstart is created. 2) If I manually create the folder (0777 just to be sure) no log/output file is created. However if I log to ec2-user I can see the script fire on [2345] but still no go with the IFACE=eth0. It just never seems to fire. Thx Art – art vanderlay Jun 24 '14 at 15:47
  • I updated my answer to address that particular EC2 instance. – grekasius Jun 24 '14 at 19:22
2

Your example is realllly close.

Amazon Linux calls their networking service network. So if the application/service you're attempting to control with upstart requires a network connection, do this:

start on (runlevel [345] and started network)
stop on (runlevel [!345] or stopping network)

You don't need the runlevel bits, but its not a bad idea to be able to limit the service from running depending on the run level environment.

Rican7
  • 121
  • 3