Ubuntu upstart init script not working - linux

I'm following an nginx tutorial from here and I created a myproject.conf file in /etc/init/ as stated in the tutorial. The code is as follows:
description "uWSGI server instance configured to serve myproject"
start on runlevel [2345]
stop on runlevel [!2345]
setuid ec2-user
setgid www-data
env PATH=/home/ec2-user/login_test/venv/bin
chdir /home/ec2-user/login_test/oauth_login
exec uwsgi --ini wsgi.ini
When I tried to activate this by giving sudo start myproject it returns:
start: Unknown job: myproject
How can I get this to work?

Related

Docker: cant access env variables inside pm2 nodejs code

So I am facing a weird problem. I am starting a docker container with a entrypoint file and a env variable edgeboxId. The entrypoint.sh contains some pm2 commands to start my nodejs application.
Entrypoint.sh
#!/bin/bash
sudo -u deploy bash << EOF
cd /home/deploy/ce-edgebox-agent
pm2 start ecosystem.json
EOF
echo "out of the deploy user"
echo Entering Entryopint file
echo "export edgeboxId=$edgeboxId">> /etc/bash.bashrc
sudo -u edgebox bash << EOF
pm2 start ce-edgebox-application --update-env
EOF
echo "out of application user"
sleep infinity
Inside dockerfile, I am using following line to import entrypoint.sh file.
RUN ["chmod", "+x", "./entrypoint.sh"]
ENTRYPOINT [ "/bin/bash", "./entrypoint.sh" ]
Then i enter inside container using:
docker exec -it <containerId> bash
Expectation:
I expect edgeboxId accessible inside nodejs application running inside pm2 process as soon as the the container starts with my entrypoint.sh file.
Whats really happening:
edgeboxId appears undefined inside nodejs application but when i run the command pm2 restart ce-edgebox-application --update-env manually inside container then edgeboxId is accessible inside nodejs application.
Question:
How to make edgeboxId accessible inside nodejs as soon as the container starts with my entrypoint.sh file?

Start a Docker container at startup in Linux on Azure

I have a Linux Virtual Machine on Azure. On this machine I installed Docker. At the startup, I want to run a Docker container. For that, I created a startup_script.sh in the tmp folder with this content
sudo docker run -d -p 8787:8787 -e USER=rstudio
-e PASSWORD=mypassword myacr.azurecr.io/mycontainer
then I run this command
chmod u+x /tmp/startup_script.sh
Then, under etc/systemd/system I created a service
[Unit]
Description=Run script at startup after network becomes reachable
After=default.target
[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/startup_script.sh
TimeoutStartSec=0
[Install]
WantedBy=default.target
Then, run
systemctl daemon-reload
systemctl enable run-at-startup.service
When I restart the machine, the Docker container is not running.
Docker recommends that you use its restart policies, and avoid using process managers like systemctl to start containers (https://docs.docker.com/config/containers/start-containers-automatically/).
First, you need to make sure that Docker Daemon (i.e. Docker service) start on boot.
On Debian and Ubuntu, the Docker service is configured to start on boot by default. To automatically start Docker and Containerd on boot for other distros, use the commands below:
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
If you're on Windows, make sure that you ticked Start Docker Desktop when you log in in Docker Desktop settings.
Then, for each container you want to start on boot, you need to use the --restart flag when running the container, e.g.:
sudo docker run --restart always -d -p 8787:8787 -e USER=rstudio
-e PASSWORD=mypassword myacr.azurecr.io/mycontainer

Running npm start from ExecStart in systemctl service file

I have the following service file:
[Unit]
Description=MyApp
After=syslog.target network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
WorkingDirectory=/opt/nodejs-sites/MyApp
ExecStart=/usr/bin/npm start
Environment=NODE_ENV=development
User=root
Group=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp
[Install]
WantedBy=multi-user.target
Here is the error from /var/log/syslog
Oct 14 13:00:55 devu18 systemd[1]: Started myapp.
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Changing to the requested working directory failed: No such file or directory
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Failed at step CHDIR spawning /usr/bin/npm: No such file or directory
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Main process exited, code=exited, status=200/CHDIR
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Failed with result 'exit-code'.
I for the life of me can't figure out why it's complaining of cannot find the file. npm start from the same working directory works just fine, no problems. Am I missing some permissions or +x or something somewhere?
This error might happen due to the fact that the executable was run without environment.
Here is a quick fix recipe
You can fix that by creating a Bash Script to do everything you need.
script.sh
#! /bin/bash
source ${HOME}/.bashrc
cd /absolute/path/to/my/project
export NODE_ENV=development
npm start
Now change it's mode to be executable
chmod +x script.sh
Now we can create our service (located for instance in /etc/systemd/system/my-project.service)
my-project.service
[Unit]
Description=My Project
After=syslog.target network.target
[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/path/to/my/script.sh
User=root
[Install]
WantedBy=multi-user.target
And now let's run it and enable it
systemctl start my-project
systemctl enable my-project
Troubleshooting (Ubuntu 18.04 LTS)
This happened to me lately that for some reason systemctl couldn't effectively source .bashrc leaving me with an error npm error does not exists This is a workaround that helped me out.
Change the script.sh file's content into the following
#! /bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
cd /absolute/path/to/my/project
export NODE_ENV=development
npm start
Now your npm service should hopefully work.
This solution was tested on:
Ubuntu 20.04 LTS, with Node version 14.16.0 installed via NVM (Node Version Manager)
Ubuntu 18.04 LTS, with Node version 12.22.6 installed via NVM (Node Version Manager)
CentOS 7, with Node version 12.22.6 installed via NVM (Node Version Manager)
/bin/npm --prefix $PATH start
This worked for me

Gitlab CI - gitlab-runner run as root

I new on continous integration on iOS,
I try to run build with gitlab-runner and use shell as executor but I got issue that pod cannot run as root I am sure that I am not installing cocoapods with sudo and I try run whoami at before_script and that's right my runner run as root
any one got same issue ?and how to fix it ?
Register the runner without sudo, and that should set the gitlab-runner to run as your current user.
So steps should be:
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
gitlab-runner register ...
gitlab-runner install
Remember to stop your sudo gitlab-runner service otherwise you could have multiple runners on the same machine fighting for the same jobs.
Here is documentation for how to use sudo and gitlab-runner user.
I am not sure, but I think it creates multiple runners.
On CentOS 8 I modified the gitlab-runner.service and changed the --user option to root.
Here is the default configuration:
/usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner
or
root#server# cat /etc/systemd/system/gitlab-runner.service
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/bin/gitlab-runner
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "gitlab-runner"
Restart=always
RestartSec=120
[Install]
WantedBy=multi-user.target
and I changed to this:
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/bin/gitlab-runner
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "root"
User=root
Group=root
Restart=always
RestartSec=120
[Install]
WantedBy=multi-user.target
So this part --user gitlab-runner to --user root
NOTE
Absolutely I did not have security concerns, and did it for test, plase make sure you are considering security part.

Node.js app can't access any env variables when pm2 is started from a script but can when launched from ssh

I am trying to launch a node.js app on a production EC2 server with pm2 process manager.
When I ssh into the instance and run pm2 start app.js, PM2 starts just fine and has access to all environment variables. Everything good.
However, I want to run pm2 start app.js from a Codedeploy hook script called applicationstart.sh, the app fails with an errored status becasue it is missing all environment variables.
Here is where the script is added so it is launched with each deployment and calls pm2 start: appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/teller-install
hooks:
AfterInstall:
- location: scripts/afterinstall.sh
timeout: 1000
runas: root
ApplicationStart:
- location: scripts/applicationstart.sh
timeout: 300
runas: ubuntu
Here is the applicationstart script:
#!/bin/bash
echo "Running Hook: applicationstart.sh"
cd /home/ubuntu/teller-install/Server/
pm2 start app.js
exit 0
I am logged in as ubuntu when I run the script from ssh and I set the script in the appconfig.yml to run as ubuntu as well.
Why would there be any difference between starting pm2 from terminal and starting it from a launch hook script?
Here is running directly from ssh:
I can provide any information necessary in dire need of solution. Thanks!
I had to add source /etc/profile before I call pm2 start app.js to load in environment variables.
The script now looks like
#!/bin/bash
echo "Running Hook: applicationstart.sh"
cd /home/ubuntu/teller-install/Server/
source /etc/profile
pm2 start app.js
exit 0

Resources