Every morning, I analyzed the stock price movement on the previous day and prepared an environment to run a program that automatically sends the results by e-mail. I will summarize what I did until I was able to start the program automatically.
It is separated from the OS that is usually used for stable operation. I used Gparted to install about 200GB of Ubuntu from the 2TB hard disk I was using for data storage. (* Be careful when partitioning an HDD that already contains data, as there is a risk of data corruption)
Ubuntu https://www.ubuntulinux.jp/download Gparted http://www.gigafree.net/system/drive/gparted.html
Installation procedure etc. omitted
When Ubuntu is installed, a boot loader called GRUB is also installed, so change the GRUB settings and set the default boot OS to Ubuntu. However, basically, Ubuntu should be the default, so unless you install 3 or more OSs or use another boot loader, it will remain as it is.
The method changes depending on the BIOS, and there are some that cannot be started automatically, but in my environment it was as follows.
I couldn't specify the day of the week, so I specified only the time. There is nothing special to do on Saturdays and Sundays, but I compromised by determining the day of the week in the python program and shutting it down on Saturdays and Sundays ...
In the case of Linux, it seems that you should put the file you want to execute first when the OS starts up in /etc/profile.d/. Is it the startup folder in Windows?
First of all, since you have the authority to create the file, as follows
$ cd /etc/profile.d/
$ sudo gedit auto_start.sh
The shell script is as follows
auto_start.sh
#!/bin/sh
cd "Enter the path to the python file"
python analysis.py
You can execute python by directly specifying the absolute path without moving with cd here, but if you do so, the relative path to be handled when importing the csv save destination or multiple files in the python program will be / etc. It will be treated based on /profile.d/. Therefore, I was able to avoid such a problem by moving the current directory to the folder of the target file and then executing it.
However, if you do this, the current directory when you start the terminal will be the folder of the python file, so you need to restore it properly at the end.
auto_start.sh
#!/bin/sh
cd "Enter the path to the python file"
python analysis.py
cd
Run the following shell script to shut it down.
shutdown.sh
#!/bin/sh
sudo shutdown -h now
However, this alone does not work. Normal use of the sudo command requires the user to manually enter the password. This makes it meaningless to automate, so the shutdown command eliminates the need to enter a password.
Edit the sudo config file there. (* Of course, it's a terrible act, so at your own risk)
$ sudo gedit /etc/sudoers
And add the following contents to the end of the file.
/etc/sudoers
"username" ALL=(ALL) NOPASSWD:/sbin/shutdown -h now
After that, you can call this shell script after the target processing is completed on the python side.
analysis.py
import subprocess
#Thing you want to do
subprocess.call("./shutdown.sh")
Now that we've created the files, give execute permission to the shell script file and the python file. If you don't, you will get an error saying that you do not have permission when executing automatically.
$ cd /etc/profile.d/
$ sudo chmod a+x auto_start.sh
$ cd "Path to python file"
$ sudo chmod a+x analysis.py
$ sudo chmod a+x shutdown.sh
Even when logging in for maintenance, it will be a problem if the process is executed and even shut down, so avoid that.
In the case of my program, it starts at 8:00 every morning and the processing is completed by 8:30, so I try to execute the processing only when it is within this time.
analysis.py
import datetime
d = datetime.datetime.today()
if (d.hour == 8) and (0 <= d.minute <= 30):
#Thing you want to do
#Shutdown process
$ sudo shutdown -c
Since it is for personal use, there are many places where the lack of knowledge is forcibly compensated. If anyone knows a better way, I would appreciate it if you could teach me.
Recommended Posts