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.