Auto startup my source code in ubuntu - linux

My python source file automatically starts while I boot the system. I tried in init.d and some other shell scripts, but its not working. Note (I also make the executable file), but it doesn't work.
Please help.

There are a number of other posts covering this topic, notably here
Put this in /etc/init (Use /etc/systemd in Ubuntu 15.x)
mystartupscript.conf
start on runlevel [2345] stop on runlevel [!2345] exec /path/to/script.py
By placing this conf file there you hook into ubuntu's upstart service that runs services on startup.
manual starting/stopping is done withsudo service mystartupscript start and sudo service mystartupscript stop
There is another solution here also also

Related

How to create upstart script for grafana service?

In my usecase, I am trying to write an upstart script for grafana service. Here is my content,
#grafana upstart script
description "start and stop grafana server"
start on (net-device-up
and local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
respawn limit 5 30
console output
exec service grafana-server stop
But this is not woking all the time. Do I need remove any pid after stopping the service?
Also is this the right way to create upstart conf with service. Because I tried to do the same for nginx service. But it fails because we need to remove the pid after stopping the service.
So in general how can we write upstart script for services?
I am using amazon linux [ec2]. Could someone help me with this?
You can just look at any script in the folder /etc/init.d and change the instructions according your your needs. The you can use the systemctl (or the appropiate command for your distribution) to make it run on the desired init levels

How to start the upstart process for golang application?

I am using upstart to start my golang application. I have my application folder structure like this,
web-app/
/app
main.go
I built the application as below,
$cd /home/ec2-user/go/src/github.com/dineshappavoo/web-app/app/
$go build ./...
It generated the binary app
And placed the web-app.conf in /etc/init/ folder. Here is the web-app.conf content,
#Web app upstart script
description "start and stop web app"
start on (net-device-up
and local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
respawn limit 5 30
console output
script
chdir /home/ec2-user/go/src/github.com/dineshappavoo/web-app/app
exec ./app
end script
When I tried sudo initctl list, it lists the process as stop/waiting. And I tried to start the process
$sudo initctl start web-app
It shows the process as start/running. But it is not started.
I checked the /var/log/messages logs. It shows,
init: web-app main process (18740) terminated with status 127
I couldn't start the process. I think there is some issue with the chdir. I tried different options for past two days. And I am fairly new to upstart but no luck. Could someone help me with this?
OP eventually solved after fixing a few issues. See comments, notably:
upstart might not recognise environment variables
Amazon Linux Image currently uses an old version of init (upstart 0.6.5), lacking newer features such as console log & nested script tags
status 127 can occur if exec can't find the binary
status 1 can occur if binary runs but fails
substituting a simple program in an upstart script can help diagnose errors

How to run a go app continously on an Ubuntu server

Couldn't seem to find a direct answer around here.
I'm not sure if I should run ./myBinary as a Cron process or if I should run "go run myapp.go"
What's an effective way to make sure that it is always running?
Sorry I'm used to Apache and Nginx.
Also what are best practices for deploying a Go app? I want everything (preferably) all served on the same server. Just like how my development environment is like.
I read something else that used S3, but, I really don't want to use S3.
Use the capabilities your init process provides. You're likely running system with either Systemd or Upstart. They've both got really easy descriptions of services and can ensure your app runs with the right privileges, is restarted when anything goes down, and that the output is are handled correctly.
For quick Upstart description look here, your service description is likely to be just:
start on runlevel [2345]
stop on runlevel [!2345]
setuid the_username_your_app_runs_as
exec /path/to/your/app --options
For quick Systemd description look here, your service is likely to be just:
[Unit]
Description=Your service
[Service]
User=the_username_your_app_runs_as
ExecStart=/path/to/your/app --options
[Install]
WantedBy=multi-user.target
You can put it in an inifiny loop, such as:
#! /bin/sh
while true; do
go run myapp.go
sleep 2 # Just in case
done
Hence, once the app dies due some reason, it will be run again.
You can put it in a script and run it in background using:
$ nohup ./my-script.sh >/dev/null 2>&1 &
You may want to go for virtual terminal utility like screen here. Example:
screen -S myapp # create screen with name myapp
cd ... # to your app directory
go run myapp.go # or go install and then ./myappfrom go bin dir
Ctrl-a+d # to go out of screen
If you want to return to the screen:
screen -r myapp
EDIT: this solution will persist the process when you go out of terminal, but won't restart it when it'll crash.

Start and Stop script of ubuntu 12.04

I have a script (twoRules.sh) which add rules to ovs plugin bridge.
The rules gets deleted when someone does service neutron-plugin-openvswitch-agent restart or reboots the system. So where should I put my scripts so that after the restart of neutron-plugin-openvswitch-agent the (twoRules.sh) scripts get executed successfully and rules remain added.
I tried putting it in /etc/init.d/neutron-plugin-openvswitch-agent file as other people suggested but this file is only called on /etc/init.d/neutron-plugin-openvswitch-agent restart and not on service neutron-plugin-openvswitch-agent restart.
You have to convert the script to a a SysV-style init script. There are many documents out there explaining about this.
http://www.debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian
http://www.cyberciti.biz/tips/how-to-controlling-access-to-linux-services.html
https://wiki.debian.org/Daemon
This way you can configure the script to be executed after certain services start or stop or when runlevel changes.

Ubuntu upstart can not stop/restart graphite carbon-cache

I created upstart config file at: /etc/init/carbon-cache.conf to stop/start/restart carbon-cache process. I can start carbon-cache process using command: start carbon-cache, however, I could not use stop/restart carbon-cache and always gives me errors: "stop: Unknown instance:".
Does anyone know what seem to be the issue? Here is the my upstart config: /etc/init/carbon-cache.conf
description "Daemonized Carbon-Cache"
start on runlevel [2345]
stop on runlevel [016]
setuid www-data
setgid www-data
exec /opt/graphite/bin/carbon-cache.py start
respawn
respawn limit 10 5
I suggest using this carbon-cache.conf file: https://gist.github.com/dbeckham/8057390
i think what's happening is that your upstart is successfully able to exec it, but as soon as it runs, carbon-cache, because of it's daemonic nature detaches itself from upstart. So when upstart tries to kill it, it realizes that carbon-cache is no longer attached.
Upstart expects the command run to stay in the foreground, not fork-off and de-attach.
"Twistd, the utility used to daemonize carbon-cache supports a --nodaemon flag that launches the process in the foreground instead of forking it into the background. At the time this article was posted, the only way to get the --nodaemon flag to twistd was by starting carbon-cache with --debug."
Though, i'd advice against un-daemoning carbon, which is necessary in an upstart implementation.
sudo /opt/graphite/bin/carbon-cache.py start

Resources