2

I have followed a few posts on here trying to run either a python or shell script on my ec2 instance after every boot not just the first boot.

I have tried the:

[scripts-user, always] to /etc/cloud/cloud.cfg file

  • Added script to ./scripts/per-boot folder

and

adding script to /etc/rc.local

  • Yes the permissions were changed to 755 for /etc/rc.local

I am attempting to pipe the output of the file into a file located in the /home/ubuntu/ directory and the file does not contain anything after boot.

If I run the scripts (.sh or .py) manually they work.

Any suggestions or request for additional info to help?

SChand
  • 51
  • 1
  • 9

3 Answers3

1

So the current solution appears to be a method I wrote off in my initial question post as I may have not performed the setup exactly as outline in the link below...

This link --> How do I make cloud-init startup scripts run every time my EC2 instance boots?

The link shows how to modify the /etc/cloud/cloud.cfg file to update scripts-user to [scripts-user, always]

Also that link says to add your *.sh file to /var/lib/cloud/scripts/per-boot directory.

Once you reboot your system your script should have executed and you can verify this in: sudo cat /var/log/cloud-init.log

if your script still fails to execute try to erase the instance state of your server with the following command: sudo rm -rf /var/lib/cloud/instance/*

--NOTE:-- It appears print commands from a python script do not pipe (>>) as expected but echo commands pipe easily

Fails to pipe sudo python test.py >> log.txt

Pipes successfully echo "HI" >> log.txt

Community
  • 1
  • 1
SChand
  • 51
  • 1
  • 9
0

Is this something along the lines that you want?

It copies the script to the instance, connects to the instance, and runs the script right away.

ec2 scp ~/path_to_script.py : instance_name -y && ec2 ssh instance_name -yc "python script_name.py" 1>/dev/null
gold_cy
  • 13,648
  • 3
  • 23
  • 45
  • do i need to download any software packages to my EC2 Ubuntu instance? do i run this only once in the terminal? or does it have to be run every time the instance reboots? – SChand Mar 27 '17 at 21:17
  • You should have Python and whatever else you need on your instance. You run this from the CLI. It depends on the script and what it is supposed to accomplish. If it is simply solving for something it only needs to be run once and this command will boot up the instance and run it right away. Try it out on a simple script and see for yourself. – gold_cy Mar 27 '17 at 21:20
  • after i run your suggested command will the script continue to run after a reboot? my issue is i need the script to run even after the first launch – SChand Mar 27 '17 at 21:28
  • i added the script to user-data but after the first launch the script no longer runs – SChand Mar 27 '17 at 21:28
  • then you need `cron` in that case, http://docs.aws.amazon.com/opsworks/latest/userguide/workingcookbook-extend-cron.html – gold_cy Mar 27 '17 at 21:31
  • does the cron job have to be executed at Launch of the instance or can it be executed at any time after Launch? – SChand Mar 27 '17 at 21:54
  • Have you gotten this to work on the EC2 Ubuntu instance? It isn't functioning as I would hope. It works on my Raspberry Pi but not the AWS EC2 instance – SChand Mar 28 '17 at 02:35
  • The code I gave you? I use some variation of it all the time. – gold_cy Mar 28 '17 at 02:37
  • oh no just in general has it worked on the ec2 ubuntu instance before? I am reading lots of different issues and solutions but haven't got one to work for me yet. Maybe I am entering the cron job incorrectly – SChand Mar 28 '17 at 18:42
0

I read that the use of rc.local is getting deprecated. One thing to try is a line in /etc/crontab like this:

@reboot full-path-of-script

If there's a specific user you want to run the script as, you can list it after @reboot.

bootbeast
  • 31
  • 2
  • does the @reboot go at last line of file? also if I run a shell script would it be "@reboot /path/to/file" ? or "@reboot sh /path/to/file" ? – SChand Mar 27 '17 at 22:01
  • does this need to be done at the instances FIRST launch? or can I do this at any point after the first launch? – SChand Mar 27 '17 at 22:02
  • my command for /etc/crontab on the last line is: @reboot /path/to/file.sh >> /path/to/log.txt after reboot there were no contents found in the .txt file... I don't think the crontab method worked unless there is a step i missed – SChand Mar 27 '17 at 22:26
  • It doesn't need to be at the first launch, and it shouldn't matter what position it is in /etc/crontab. Make sure crond is running and if you find that it's not at startup, enter "systemctl enable crond." – bootbeast Mar 27 '17 at 22:37
  • Also, in your script it may help to specify the $PATH that you have when the script is working manually. For example, do echo $PATH ...on the command line and have the output in your script. For example: PATH=/sbin:/bin:/usr/sbin:/usr/bin I can think of times when a script ran but did nothing because the environment variable of the right paths wasn't there. ...and under /var/log there should be a cron log file that lists the event of cron jobs running. – bootbeast Mar 27 '17 at 22:40
  • the ec2 ubuntu says `Failed to execute operation: No such file or directory` when I run the systemctl enable crond... is the ec2 ubuntu instance not configured for this cron setup? running "systemctl enable crond" without 'sudo' required authentication...after entering password`Failed to execute operation: Access denied` – SChand Mar 27 '17 at 23:06
  • Maybe for your distro it's "sudo systemctl enable cron" but it's only necessary if it's not running at startup. There should be a cron log under /var/logs that will show which jobs have run. – bootbeast Mar 28 '17 at 01:32
  • I am using full paths for all commands and cron is still not working. Have you been able to successfully get this to work on the EC2 Ubuntu instance? `/usr/bin/python /home/ubuntu/test.py >> /home/ubuntu/log.txt` – SChand Mar 28 '17 at 02:31
  • I don't see a copy of your script above, so I can't test it. I did test and find that the user is required in the /etc/crontab entry before the path & script to be run. This made my script run once after the reboot command. – bootbeast Mar 28 '17 at 15:56
  • So all I have done is edit /etc/crontab to contain `@reboot root /path/to/file.sh` and I reboot my system **file.sh contains a print command that is piped (>>) into a log.txt** but the log.txt file is empty (telling me script failed to execute – SChand Mar 28 '17 at 19:17