startup scripts for amazon linux ami - linux

I'm trying to start a script every time I start my amazon micro instance (amazon linux ami).
I tried to put it in /etc/init.d/ and linkted it to /etc/rc.d/ but the script is not executed.
I've also looked at user-data scripts, but as I understand the documentation they are only executed on the first startup of the instance.
What do I have to do to run my script on every startup?

Init.d/rc.d are primarly used for services. Any scripts that you install here have to be enabled to run at boot chkconfig service on.
Since it sounds like you are really just running a one time script. You can set it up to run with cron using the #reboot flag instead of providing a time.

Related

Firecracker microVM: how to execute a script file when a Firecracker microVM startup?

I went through Get Start Guide and api_request_doc to find a way to execute a script when start a microVM? But there is not way to do it.
Is there a way to achieve it? I want to run some script when a vm startup or execute a script without login.
So, far firecracker has not provided any support to start a script on startup.
You can use system daemons like openrc (for alpine based) and systemd to start the script on the startup.
to start script using systemd on startup, check https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux

How to execute Shell Script ,while booting up EC2 Machine?

I want to execute shell script whenever I start my ec2 (Linux )instance first time
Any Suggestion Please.
For the time being you can add the entries/command in initrd file and you can create a softlink for it. This will help you to execute the shell script whenever u will start your linux machine whether it is on EC2 or on linux.
See "Running Commands on Your Linux Instance at Launch".
You can use "user data" or "cloud-init" to do it. There's a section when manually launching EC2 instances to add the user data, and you can do it through APIs too.

How to set up a bash script to run in the background anytime the linux ubuntu server is running

I have written up a simple bash script that will copy the newest image from my ip camera into a directory, rename the file and delete the old file. The script loops every 10 seconds.
I want to have this script start running in the background and run continuously all the time that the server is up.
I understand the part about adding a & to the end of the command will cause it to run in the background.
Is init.d the best place to execute this?
I am running ubuntu server.
This sort of thing is normally done by service scripts, which you would find under /etc/init.d. Depending on the version, that might be a "System V init script", or one of the systemd scripts.
A simple service script of the sort you are asking about would start automatically (based on comments in the script's header that tell what run-levels it would use), create a file under /var/run telling what process-id the script uses (to allow killing it), and run the copying in a loop, calling sleep 10 to space the timing as indicated.
A typical service script should implement "start", "stop", "restart" and "status". Not all do, but there is rarely a good reason to not do this.
On my (Debian) system, there is a README file in the directory which is a good introduction to the topic. There are several tutorials available for the topic. Here are a few:
Linux: How to write a System V init script to start, stop, and restart my own application or service
Writing a Linux Startup Script
Manage System Startup and Boot Processes on Linux with Upstart

Linux Invoke custom script during OS shutdown

I wrote a simple JAVA application which runs as a service. When my application is up and running, I maintain the PID in a file which will be used while stopping the application.
Issue:
When I restart the OS the stop script is not called (not sure how to make this happen) and the old PID is left as it is in the PID file. Now, after reboot (which start my app) when I stop the app using stop script now my stop script will try to clean up all the PID listed in the file. Most of the time, I will get "No such process". But there are chance the same PID might have been used for some other process
Question:
How I can make sure my stop script will be invoked when I shutdown ore reboot the OS? I am looking a solution for RHEL environment.
I think your are looking for a init script. (startup/shutdown services at different run levels)
This is a good reference
http://blog.rimuhosting.com/2009/09/30/one-java-init-script/
this has a good refernce to Linux init.d scripts
http://coreymaynard.com/blog/creating-a-custom-initd-script-on-fedora/
Hope it helps
If you are looking for scripts that run after reboot, I guess you can write the script in /etc/rc.local and then you can start your service.
This script will run after all your init scripts have run while your machine starts. Using this you can delete the old PID file.

On OpenSuSE (or linux in general) where should a non-service startup script go?

On an OpenSuSE linux machine, I want to run a script when the machine is booted. As the script does not start a service, is /etc/rc.d the correct place for the script?
Thanks.
That's where I've always put them. There are several scripts in rc.d already which don't actually start a process. However, you need to write it in a similar fashion to the other startup scripts - have a look at a few to see how they are set up. Specifically, you should make sure that it's possible to run the script using /etc/rc.d/myscript start.

Resources