How to start the PC at a fixed time every morning and execute the python program

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.

procedure

  1. Partition and install Ubuntu
  2. Set the default boot OS to Ubuntu with the boot loader
  3. Set the BIOS to boot at the specified time every morning
  4. Create a shell script to start python in /etc/profile.d/
  5. Create a shell script to shut down after execution
  6. Give execute permission to the file
  7. Settings for normal startup

Actual flow

Partition and install Ubuntu

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

Default boot OS to Ubuntu with boot loader

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.

Set to boot at the specified time every morning in the BIOS

The method changes depending on the BIOS, and there are some that cannot be started automatically, but in my environment it was as follows. IMG.jpg

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 ...

Create a shell script to start python in /etc/profile.d/

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

Create a shell script that shuts down after execution

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")

Give execute permission to the file

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

Settings for normal startup

Even when logging in for maintenance, it will be a problem if the process is executed and even shut down, so avoid that.

Specify time with python

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

When you want to avoid only shutdown

$ sudo shutdown -c

Finally

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

How to start the PC at a fixed time every morning and execute the python program
How to execute a schedule by specifying the Python time zone and execution frequency
How to stop a program in python until a specific date and time
How to start the program
How to start a simple WEB server that can execute cgi of php and python
How to insert a specific process at the start and end of spider with scrapy
How to batch start a python program created with Jupyter notebook
How to get the date and time difference in seconds with python
[Python] How to open two or more files at the same time
[Python] How to save the installed package and install it in a new environment at once Mac environment
I want to make a music player and file music at the same time
A program that sends a fixed amount of mail at a specified time by Python
Think about how to program Python on the iPad
Specifies the function to execute when the python program ends
Tasks at the start of a new python project
How to execute a command using subprocess in Python
How to unit test a function containing the current time using freezegun in python
[Mac] A super-easy way to execute system commands in Python and output the results
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
How to run a Python file at a Windows 10 command prompt
How to run a Python program from within a shell script
How to shuffle a part of a Python list (at random.shuffle)
How to use the __call__ method in a Python class
Build a Python environment and transfer data to the server
How to get the last (last) value in a list in Python
How to write a metaclass that supports both python2 and python3
How to execute external shell scripts and commands in python
An easy way to view the time taken in Python and a smarter way to improve it
[Python] A program to find the number of apples and oranges that can be harvested
I want to get information from fstab at the ssh connection destination and execute a command
Determine the date and time format in Python and convert to Unixtime
I want to record the execution time and keep a log.
How to determine the existence of a selenium element in Python
[Introduction to Python] How to split a character string with the split function
How to get followers and followers from python using the Mastodon API
How to check the memory size of a variable in Python
[Python] How to get the first and last days of the month
How to make a surveillance camera (Security Camera) with Opencv and Python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
[Python3] Define a decorator to measure the execution time of a function
Try to write a program that abuses the program and sends 100 emails
The road to installing Python and Flask on an offline PC
How to send a request to the DMM (FANZA) API with python
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
[Python] A program that rotates the contents of the list to the left
How to display bytes in the same way in Java and Python
How to write a Python class
How to get the Python version
[Python] How to import the library
How to read standard input or variable files at the same time like paste command in Python
How to input a character string in Python and output it as it is or in the opposite direction.
[Python Kivy] How to get the file path by dragging and dropping
[Python] How to specify the window display position and size of matplotlib
How to divide and process a data frame using the groupby function
[Python] What is a formal argument? How to set the initial value
[Introduction to Python] What is the difference between a list and a tuple?
How to write the correct shebang in Perl, Python and Ruby scripts
[Python] Explains how to use the range function with a concrete example
[Python] A program that calculates the number of socks to be paired