Share Cách tự động kích hoạt File Python khi cắm USB

Raspberry Pi ** Cách chạy mã Python khi ổ USB được cắm vào Raspberry Pi **

Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách chạy tập lệnh Python khi ổ USB được cắm vào Raspberry Pi.Đây là một cách tuyệt vời để tự động chuyển các tệp đến hoặc từ Raspberry Pi của bạn hoặc để chạy tập lệnh khi bạn kết nối một loại ổ USB cụ thể.

** Điều kiện tiên quyết **

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một quả mâm xôi pi
* Ổ USB
* Một trình giả lập thiết bị đầu cuối (như putty hoặc thiết bị đầu cuối)
* Ngôn ngữ lập trình Python

** Bước 1: Tạo tập lệnh Python **

Bước đầu tiên là tạo tập lệnh Python sẽ chạy khi ổ USB được cắm vào. Tập lệnh này có thể làm bất cứ điều gì bạn muốn, nhưng đối với hướng dẫn này, chúng tôi sẽ chỉ cần in một thông báo vào thiết bị đầu cuối.

Mở trình soạn thảo văn bản và tạo một tệp mới có tên là `usb.py`.Trong tệp này, thêm mã sau:

`` `Python
Nhập hệ điều hành
Thời gian nhập khẩu

def main ():
# Đợi ổ USB được cắm vào
thời gian.s ngủ (10)

# Nhận đường dẫn đến ổ USB
Drive = Os.Path.Join ('/Media', Os.ListDir ('/Media') [0])

# In một tin nhắn vào thiết bị đầu cuối
print ('ổ USB được kết nối tại {}'. định dạng (ổ đĩa)))

Nếu __name__ == '__main__':
chủ yếu()
`` `

** Bước 2: Tạo dịch vụ Systemd **

Bước tiếp theo là tạo một dịch vụ SystemD sẽ chạy tập lệnh Python khi ổ USB được cắm vào. Để thực hiện việc này, hãy mở một thiết bị đầu cuối và chạy lệnh sau:

`` `
sudo nano /etc/systemd/system/usb.service
`` `

Điều này sẽ mở một tệp mới trong trình chỉnh sửa.Trong tệp này, thêm mã sau:

`` `
[Đơn vị]
Mô tả = Chạy tập lệnh Python khi ổ USB được cắm vào
After = multi-user.target

[Dịch vụ]
Loại = đơn giản
ExecStart =/usr/bin/python3 /path/to/usb.py

[Cài đặt]
Wantby = multi-user.target
`` `

Thay thế `/path/to/usb.py` bằng đường dẫn đến tập lệnh python bạn đã tạo trong bước trước.

** Bước 3: Kích hoạt dịch vụ Systemd **

Bây giờ bạn đã tạo dịch vụ SystemD, bạn cần bật nó để nó tự động chạy khi ổ USB được cắm vào. Để thực hiện việc này, hãy chạy lệnh sau:

`` `
sudo systemctl cho phép usb.service
`` `

** Bước 4: Kiểm tra dịch vụ Systemd **

Bây giờ bạn đã bật dịch vụ SystemD, bạn có thể kiểm tra nó bằng cách cắm vào ổ USB.Nếu tập lệnh Python chạy thành công, bạn sẽ thấy một thông báo trong thiết bị đầu cuối có nội dung `ổ USB được kết nối tại {}`.

**Phần kết luận**

Trong hướng dẫn này, bạn đã học cách chạy tập lệnh Python khi ổ USB được cắm vào Raspberry Pi.Đây là một cách tuyệt vời để tự động chuyển các tệp đến hoặc từ Raspberry Pi của bạn hoặc để chạy tập lệnh khi bạn kết nối một loại ổ USB cụ thể.
=======================================
raspberry pi **How to run Python code when a USB drive is plugged into Raspberry Pi**

In this tutorial, I will show you how to run a Python script when a USB drive is plugged into a Raspberry Pi. This is a great way to automatically transfer files to or from your Raspberry Pi, or to run scripts when you connect a specific type of USB drive.

**Prerequisites**

To follow this tutorial, you will need the following:

* A Raspberry Pi
* A USB drive
* A terminal emulator (such as PuTTY or Terminal)
* The Python programming language

**Step 1: Create a Python script**

The first step is to create a Python script that will run when the USB drive is plugged in. This script can do anything you want, but for this tutorial, we will simply print a message to the terminal.

Open a text editor and create a new file called `usb.py`. In this file, add the following code:

```python
import os
import time

def main():
# Wait for the USB drive to be plugged in
time.sleep(10)

# Get the path to the USB drive
drive = os.path.join('/media', os.listdir('/media')[0])

# Print a message to the terminal
print('USB drive connected at {}'.format(drive))

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

**Step 2: Create a systemd service**

The next step is to create a systemd service that will run the Python script when the USB drive is plugged in. To do this, open a terminal and run the following command:

```
sudo nano /etc/systemd/system/usb.service
```

This will open a new file in the editor. In this file, add the following code:

```
[Unit]
Description=Run a Python script when a USB drive is plugged in
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /path/to/usb.py

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

Replace `/path/to/usb.py` with the path to the Python script you created in the previous step.

**Step 3: Enable the systemd service**

Now that you have created the systemd service, you need to enable it so that it will run automatically when the USB drive is plugged in. To do this, run the following command:

```
sudo systemctl enable usb.service
```

**Step 4: Test the systemd service**

Now that you have enabled the systemd service, you can test it by plugging in the USB drive. If the Python script runs successfully, you should see a message in the terminal that says `USB drive connected at {}`.

**Conclusion**

In this tutorial, you learned how to run a Python script when a USB drive is plugged into a Raspberry Pi. This is a great way to automatically transfer files to or from your Raspberry Pi, or to run scripts when you connect a specific type of USB drive.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top