0

I am trying to run systemctl service in ec2 instance on launching of ec2 instance. So for to run my script on start of ec2 instance i am running my script using user data of ec2 instance. So my script has the following lines of code in bash

start.sh

#!/usr/bin/env sh
set -e

FILE=/etc/rsyslog.d/"$SERVICE_NAME".conf sudo touch "$FILE"

cat <<EOM | sudo tee "$FILE" if $programname == "$SERVICE_NAME" then "$ROUTER_LOGS" & stop EOM

ROUTER_LOGS=/var/log/router/out.log FILE=/usr/local/bin/upload_logs.sh sudo touch $FILE chmod +x $FILE S3_BUCKET=my-bucket cat <<EOM | sudo tee $FILE aws s3 cp $ROUTER_LOGS s3://$S3_BUCKET/logs/$HOSTNAME/$SERVICE_NAME.log.$(date +%Y-%m-%d_%H%M%S) EOM

FILE=/etc/logrotate.d/router sudo touch $FILE cat <<EOM | sudo tee $FILE /var/log/router/*.log { hourly rotate 24 missingok notifempty compress delaycompress endscript }

FILE="/etc/systemd/system/router.service" cat <<EOM | sudo tee $FILE [Unit] Description=$SERVICE_NAME After=network.target Requires=network.target

[Service] WorkingDirectory=$APP_DIR/target/release/ ExecStart=$APP_DIR/target/release/router ExecStopPost=/bin/bash /usr/local/bin/upload_logs.sh StandardOutput=syslog StandardError=syslog SyslogIdentifier=$SERVICE_NAME TimeoutStartSec=1000s Environment="RUST_LOG=info" Environment="RUST_ENV=$ENV" Environment="APP_DIR=$APP_DIR" Restart=on-failure Type=simple

[Install] WantedBy=multi-user.target

EOM

sudo systemctl daemon-reload sudo systemctl enable router.service sudo systemctl status router.service # sudo systemctl start router.service sudo systemctl status router.service

The output of the above script is

● router.service - gql-router
   Loaded: loaded (/etc/systemd/system/router.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

I am using Centos 7 linux in EC2 of AWS.

But It is working when i tried running it inside machine using ssh. So the problem is running using user data of EC2. I tried checking logs using journalctl but no entries found of the service. Can anyone please help me with this problem in solving.

Greenonline
  • 2,390

1 Answers1

0

The script will exit at any error because of the set -e.

For better troubleshooting, you need have set -x to debug and see what's happening.

Potential cause:

After=network.target Requires=network.target

It looks like the scrip needs network services to be up, and if they are not, it exits.

The fix would be to wait for the network services to come up.

For this, you need to replace these 2 lines with:

After=network-online.target Wants=network-online.target

This will make it wait for the network services to come online. It will fail (timeout) after 90 seconds.

Hope this help, and fixes your issue.

Algeriassic
  • 1,719
  • 11
  • 10