Install upstart package from repo, avaiable in both debian and centos.
To create a job to be started automatically when Ubuntu starts. As written example, suppose create the following file /etc/init/testservice.conf with sudo:
# testservice - test service job file
description "my service description"
author "Me <myself@i.com>"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
# This option does not seem to be of great importance, so it does not need to be set.
#expect fork
# Specify working directory
chdir /home/user/testcode
# Specify the process/command to start, e.g.
exec python mycommand.py arg1 arg2
To 'manually' start or stop the process use
sudo start testservice
sudo stop testservice
The above example works great for everything , but however, there is short cut too :
The solution is to use a shutdown-hook for the system. In Ubuntu it is very easy to add a shutdown-hook by using upstart.
The upstart job config file (/etc/init/shutdown-hook.conf) should look something like this:
description "run at shutdown"
start on starting rc
task
exec /bin/bash /etc/my_service/upload_log_to_s3.sh log
Finally I just needed the script (/etc/my_service/upload_log_to_s3.sh) for uploading a log file to S3:
#!/bin/bash
log_file_ext=$1
gzip -c /var/log/my_service/*.$log_file_ext > /tmp/log.gz
aws s3 cp /tmp/log.gz s3://my_service_bucket/logs/`date +%Y-%m-%dT%H:%M:%SZ`.log.gz
The upload script will just gzip the log file (needed as I’m using delaycompress), rename the log file to the current timestamp, and upload the file using aws-cli. The argument sets the file extension of the log file, which is necessary to be able to upload both the current (.log) as well as the previous log file (.log.1).
To create a job to be started automatically when Ubuntu starts. As written example, suppose create the following file /etc/init/testservice.conf with sudo:
# testservice - test service job file
description "my service description"
author "Me <myself@i.com>"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
# This option does not seem to be of great importance, so it does not need to be set.
#expect fork
# Specify working directory
chdir /home/user/testcode
# Specify the process/command to start, e.g.
exec python mycommand.py arg1 arg2
To 'manually' start or stop the process use
sudo start testservice
sudo stop testservice
The above example works great for everything , but however, there is short cut too :
The solution is to use a shutdown-hook for the system. In Ubuntu it is very easy to add a shutdown-hook by using upstart.
The upstart job config file (/etc/init/shutdown-hook.conf) should look something like this:
description "run at shutdown"
start on starting rc
task
exec /bin/bash /etc/my_service/upload_log_to_s3.sh log
Finally I just needed the script (/etc/my_service/upload_log_to_s3.sh) for uploading a log file to S3:
#!/bin/bash
log_file_ext=$1
gzip -c /var/log/my_service/*.$log_file_ext > /tmp/log.gz
aws s3 cp /tmp/log.gz s3://my_service_bucket/logs/`date +%Y-%m-%dT%H:%M:%SZ`.log.gz
The upload script will just gzip the log file (needed as I’m using delaycompress), rename the log file to the current timestamp, and upload the file using aws-cli. The argument sets the file extension of the log file, which is necessary to be able to upload both the current (.log) as well as the previous log file (.log.1).
No comments:
Post a Comment