mswhite commited on
Commit
1be04f5
·
verified ·
1 Parent(s): d0603ec

Upload shell_launch_shutdown_script.txt

Browse files
Files changed (1) hide show
  1. shell_launch_shutdown_script.txt +114 -0
shell_launch_shutdown_script.txt ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ To create a script that runs a Python program at launch, waits for it to finish, and then initiates a shutdown sequence on a Raspberry Pi, you can follow these steps:
4
+
5
+ 1. **Create the Python Program**: Ensure you have the Python program you want to run. For this example, let's assume it's named `my_program.py`.
6
+
7
+ 2. **Create a Shell Script**: This script will execute your Python program and then shut down the Raspberry Pi.
8
+
9
+ 3. **Set Up the Script to Run at Boot**: Use `cron` or `systemd` to run the script at startup.
10
+
11
+ Here's how you can implement these steps:
12
+
13
+ ### Step 1: Create the Python Program
14
+
15
+ Make sure your Python program is ready. For example, `my_program.py`:
16
+
17
+ ```python
18
+ # my_program.py
19
+ import time
20
+
21
+ def main():
22
+ print("Program started")
23
+ time.sleep(10) # Simulate a task that takes 10 seconds
24
+ print("Program finished")
25
+
26
+ if __name__ == "__main__":
27
+ main()
28
+ ```
29
+
30
+ ### Step 2: Create a Shell Script
31
+
32
+ Create a shell script that will run your Python program and then shut down the Raspberry Pi.
33
+
34
+ Create a file named `run_and_shutdown.sh`:
35
+
36
+ ```bash
37
+ #!/bin/bash
38
+
39
+ # Run the Python program
40
+ python3 /path/to/your/my_program.py
41
+
42
+ # Wait for the program to finish
43
+ wait
44
+
45
+ # Initiate shutdown
46
+ sudo shutdown -h now
47
+ ```
48
+
49
+ Make sure to replace `/path/to/your/my_program.py` with the actual path to your Python script.
50
+
51
+ ### Step 3: Make the Shell Script Executable
52
+
53
+ Run the following command to make the script executable:
54
+
55
+ ```bash
56
+ chmod +x /path/to/your/run_and_shutdown.sh
57
+ ```
58
+
59
+ ### Step 4: Set Up the Script to Run at Boot
60
+
61
+ You can use `cron` to run the script at startup:
62
+
63
+ 1. Open the crontab file for editing:
64
+
65
+ ```bash
66
+ crontab -e
67
+ ```
68
+
69
+ 2. Add the following line to the crontab file to run the script at boot:
70
+
71
+ ```bash
72
+ @reboot /path/to/your/run_and_shutdown.sh
73
+ ```
74
+
75
+ 3. Save and exit the editor.
76
+
77
+ Alternatively, you can use `systemd` to create a service that runs the script at startup:
78
+
79
+ 1. Create a new service file:
80
+
81
+ ```bash
82
+ sudo nano /etc/systemd/system/run_and_shutdown.service
83
+ ```
84
+
85
+ 2. Add the following content to the file:
86
+
87
+ ```ini
88
+ [Unit]
89
+ Description=Run Python Program and Shutdown
90
+
91
+ [Service]
92
+ ExecStart=/path/to/your/run_and_shutdown.sh
93
+ Type=oneshot
94
+ RemainAfterExit=true
95
+
96
+ [Install]
97
+ WantedBy=multi-user.target
98
+ ```
99
+
100
+ 3. Save and exit the editor.
101
+
102
+ 4. Enable the service to run at startup:
103
+
104
+ ```bash
105
+ sudo systemctl enable run_and_shutdown.service
106
+ ```
107
+
108
+ 5. Start the service (optional, for testing):
109
+
110
+ ```bash
111
+ sudo systemctl start run_and_shutdown.service
112
+ ```
113
+
114
+ This setup will ensure that your Python program runs at boot, and once it completes, the Raspberry Pi will shut down. Make sure to test the script and service to ensure everything works as expected.