File size: 2,854 Bytes
1be04f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114


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:

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

2. **Create a Shell Script**: This script will execute your Python program and then shut down the Raspberry Pi.

3. **Set Up the Script to Run at Boot**: Use `cron` or `systemd` to run the script at startup.

Here's how you can implement these steps:

### Step 1: Create the Python Program

Make sure your Python program is ready. For example, `my_program.py`:

```python
# my_program.py
import time

def main():
    print("Program started")
    time.sleep(10)  # Simulate a task that takes 10 seconds
    print("Program finished")

if __name__ == "__main__":
    main()
```

### Step 2: Create a Shell Script

Create a shell script that will run your Python program and then shut down the Raspberry Pi.

Create a file named `run_and_shutdown.sh`:

```bash
#!/bin/bash

# Run the Python program
python3 /path/to/your/my_program.py

# Wait for the program to finish
wait

# Initiate shutdown
sudo shutdown -h now
```

Make sure to replace `/path/to/your/my_program.py` with the actual path to your Python script.

### Step 3: Make the Shell Script Executable

Run the following command to make the script executable:

```bash
chmod +x /path/to/your/run_and_shutdown.sh
```

### Step 4: Set Up the Script to Run at Boot

You can use `cron` to run the script at startup:

1. Open the crontab file for editing:

   ```bash
   crontab -e
   ```

2. Add the following line to the crontab file to run the script at boot:

   ```bash
   @reboot /path/to/your/run_and_shutdown.sh
   ```

3. Save and exit the editor.

Alternatively, you can use `systemd` to create a service that runs the script at startup:

1. Create a new service file:

   ```bash
   sudo nano /etc/systemd/system/run_and_shutdown.service
   ```

2. Add the following content to the file:

   ```ini
   [Unit]
   Description=Run Python Program and Shutdown

   [Service]
   ExecStart=/path/to/your/run_and_shutdown.sh
   Type=oneshot
   RemainAfterExit=true

   [Install]
   WantedBy=multi-user.target
   ```

3. Save and exit the editor.

4. Enable the service to run at startup:

   ```bash
   sudo systemctl enable run_and_shutdown.service
   ```

5. Start the service (optional, for testing):

   ```bash
   sudo systemctl start run_and_shutdown.service
   ```

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.