How to run a command at EC2 Instance RE-boot? - linux

I had a project where I need to run a command at EC2 reboot. I found only information about User Data but that works only at first launch which is not exactly what I needed. I need a command to run everytime I connect to the machine.

You can store a shell script in this directory:
/var/lib/cloud/scripts/per-boot/
It will be automatically run after every boot. (This is done by cloud-init, which also runs User Data scripts.)

After a little bit of research, I found a very easy solution. You can simply run.
echo "YOUR_COMMAND" >> .bashrc
This works also if you write it in the User Data section when launching your EC2 instance so that you don't have to SSH into it after launch.
Enjoy!

Related

Make chosen version of Elasticsearch run as a service in Linux

I have an issue with later versions of ES, so have to use 7.10.2 currently.
This means that the previous method I used to install ES as a service, i.e. apt-get, doesn't work You can't choose an older version this way: it currently installs 7.16.3.
So I followed the procedure on this page for 7.10: everything worked: I was able to run ES as an app and also as a "daemon". Clearly I could simply put the "daemon" startup line in a script which runs on boot.
But what's the optimum way of turning this "daemon arrangement" into a service which you can control with systemctl, and which starts automatically when the machine boots?
PS I don't want to get involved with Docker. I'm sure that's a useful thing but I'm convinced there is a simpler way of doing it, using available Linux sys tools.
I found a workaround... this doesn't in fact create a service of the "systemd" type which can be controlled by systemctl. There seem to be one or two problems which make this non-trivial.
1) You can't start ES as root! I assume (not sure) that most services are being run by root. Anyway this was something I couldn't find a solution to.
2) I am not sure whether a shell script file called by a service is allowed to end... or should continue endlessly: initially I thought this would be sufficient. This is a shell script (run_es_daemon.sh) which does indeed start up ES (as a daemon process) when run by manually in a terminal. There is no issue to do with the fact that the script ends and you then close the terminal: the daemon process continues to run:
#!/bin/bash
# start ES as a daemon...
cd /home/mike/Elasticsearch/elasticsearch-7.10.2
./bin/elasticsearch -d -p pid
... but it never worked using a xxx.service file in /etc/systemd/system/ (maybe because of 1) above). So I also tried adding these lines under the above ones:
while true
do
echo "bubbles"
sleep 60
done
... didn't work either.
In the end I found a simple workaround solution was to start up the daemon process by using crontab:
#reboot /home/mike/sysadmin/run_es_daemon.sh
... but I'd still like to know how to set it up as a true service, which starts at boot...

Autostart a node.js script using init.d in Debian

I have a little node application on a server (node mailer) that I run by going to its source folder and executing npm start. I figured the best way to run this automatically would be to create a my_script.sh file and drop it in the init.d directory of my debian box. Inside the file (below the !#/bin/bash line), the code to execute is
'/opt/mycode/source/npm start'
I save the line to the .sh file and restarted the machine, but so far haven't got it to work. My question is: is this even how you start a script like this (using that command and an .sh file)? It does start normally when I do it manually (when I navigate to it and run npm start in the terminal). I included the single quotes around it because of the space between npm start. Also, if I want to verify that it worked, which process would I look for other than just pinging my smtp mailer? Finally, I know I need to run:
update-rc.d my_script.sh defaults
but I was also confused at to whether I had done this correctly either (is it just the name of the file that goes there or the file plus the extension)?
The script that you leave on the init.d folder should not have any extension and should have functions to start, stop and get the status of the service (your application).
I'll leave a link with an example as well as with some basis in order to build the Linux service script.
I would suggest reloading the daemon with systemctl daemon-reload in order to refresh the Linux service files once you add a new one.

Shell script can't find other files when launched on login (Debian Linux)

So I have a script that is launched on login via rc.local. The script calls a few other scripts to be launched and ran. However the console says that the file and/or directories could not be found. When I run the script manually after the login it works just fine. I have even tried to add a small delay so that I know that the system logged in. Any idea why this is and how to fix it?
Sorry if my answer is a bit vague but from what I understand is that the scripts require to be run via admin privileges. Let me elaborate, when the system starts it runs a set of specific scripts and this differs from distro to distro. Therefore, I'd check ~/.bashrc /etc/profile.dand most importantly ~/.bash_login. See what you can do there. Personally I added it here ~/.config/autostart worked fine. Don't forget to create a .desktop file.
Hope this helps

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.

Shared Library issues when running over SSH (linux)

I am having some difficulty running jobs over SSH. I have a series of networked machines which all have access to the same Home folder (when my executable is installed). While working on one machine I would like to be able run my code through ssh using the following sort of command:
ssh -q ExecutableDir/MyExecutable InputDir/MyInput
If I ssh in to any of the machines I wish to run the job on remotely and simply run:
ExecutableDir/MyExecutable InputDir/MyInput
It runs without fail, however when I run through SSH I get an error saying some shared libraries can't be found. Has anyone come across this sort of thing before?
ok I figured it out myself.
It seems when you run things through ssh in the way shown above you don't inherit the path variables etc. that you would if you ssh-ed in 'properly'. You can see this by running:
ssh RemoteMachine printenv
and comparing the output to what you would normally get if you were connected to the remote machine. The solution I then went for was to run something like the following:
ssh -q ExecutableDir/MyExecutable source ~/.bash_profile && InputDir/MyInput
Which then gets all the paths and stuff you might need from the bash_profile file on the remote machine

Resources