Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Raspberry pie uses minimalist Service written by Shell

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you the minimalist Service written by raspberry pie using Shell, which is concise and easy to understand. I hope you can get something through the detailed introduction of this article.

Raspberry Pi runs a Debian-based system that not only runs Shell, but also supports systemd and docker, and you can write a simple service to run at startup and perform some automated operations. This is done using shell, systemd, and Docker 18.06.1 on RaspPi Zero W. The detailed steps are described below.

1. Initialize the system

Download the image from the raspberry pie official website (https://www.raspberrypi.org/)) and burn it to the SD card (I use Etcher).

Download: https://www.raspberrypi.org/downloads/raspbian/

Benefits: an open source super easy-to-use SD card / USB disk burning tool

Insert the SD card into the raspberry pie and power it on.

Login to use: pi,raspberry.

If you want to operate remotely through ssh, you also need to install and enable sshd service boot (described below).

Of course, the first thing is to open WiFi and be able to connect to the Internet.

2. WiFi settings

Wifi AP settings, use the command "nano / etc/wpa_supplicant/wpa_supplicant.conf" to edit the configuration file, and refer to the following method to add WiFi hotspots:

Ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1network= {ssid= "HiWiFi_xxxxxx" psk= "mypassword"}

A static address is required to establish the service. Modify the file dhcpcd (using nano / etc/dhcpcd) and edit the following:

Interface wlan0noipv6rsnoipv6static ip_address=192.168.199.190static routers=192.168.199.1domain_name_servers=192.168.199.1 8.8.8.8 9.9.9.9

Change the above IP address and DNS to your own, where 8.8.8.8 take 9.9.9 is the public DNS service address and does not need to be modified.

Restart the dhcpcd service (or need to restart raspberry pie).

After the networking is successful, perform the software version update operation. As follows:

Sudo apt update & & sudo apt upgrade-y3, writing WatchDog and Service

Create your own directory and create the following three files:

Sshp.service, the service profile invoked by systemd.

Sshp.daemon, the script file that executes the service control (start, stop, status).

Sshp.worker, the task script file for the execution service.

The contents of the document are as follows.

3.1 sshp.service file [Unit] Description=Secure Shell server proxyAfter= network.target] Type=forkingExecStart=/home/pi/openthings/tutools/sshp.daemon startExecStop=/home/pi/openthings/tutools/sshp.daemon stopTimeoutSec=0RemainAfterExit= Yes [install] WantedBy=multi-user.targetAlias=sshp.service3.2 sshp.daemon file #! / bin/shecho "SSHP tools." # = start () {now= `date'+% Y-%m-%d% now Start SSHP worker% Mvana% S``date "$now Start SSHP worker." > > / home/pi/openthings / tutools/sshp.log nohup / home/pi/openthings/tutools/sshp.worker &} # = = stop () {PID= `ps-aux | grep "sshp.worker" | grep-v grep | awk-F "'{print $2} '`kill-9$ PID echo" Stop SSHP worker PID= "$PID > > / home/pi/openthings/tutools/sshp.log} # = restart () {echo" Restart Service... " Stop Start echo "Restarted SSHT worker, PID=" $PID} # = = status () {now= `date'+% Y-%m-%d% HV% MV% S '`echo "$now Service status ssht." Ps-aux | grep "sshp.worker" | grep-v grep} case "$1" in start) start;; stop) stop;; restart) restart;; status) status; *) echo "Usage: (start | stop | restart | status)" exit 1;; esacecho "Service SSHP tools done." exit 03.3 sshp.worker file #! / bin/shwhile true; do sshpass-p mypassword ssh-o "ExitOnForwardFailure yes"-p 22000-NgD *: 5555 root@myserverdone

Here only ssh is used to demonstrate the worker writing method. For more information, please refer to: SSH command line parameter detailed explanation [English]

The trap here is that the account at startup of the service is root, which is different from the configuration information and default parameters of the current user pi.

3.4 set to run at startup

Use the following command to set the service you just wrote to run at startup:

Sudo systemctl enable sshp.service

Take a look at the startup service in the system:

# ls-l / etc/systemd/system/multi-user.target.wants/total 0lrwxrwxrwx 1 root root 40 Nov 29 2017 avahi-daemon.service-> / lib/systemd/system/avahi-daemon.servicelrwxrwxrwx 1 root root 41 Nov 29 2017 console-setup.service-> / lib/systemd/system/console-setup.servicelrwxrwxrwx 1 root root 38 Apr 12 05:45 containerd.service-> / lib/systemd/system/containerd.servicelrwxrwxrwx 1 root root 32 Nov 29 2017 cron.service-> / lib/systemd/system/cron.servicelrwxrwxrwx 1 Root root 34 Nov 29 2017 dhcpcd.service-> / lib/systemd/system/dhcpcd.servicelrwxrwxrwx 1 root root 34 Dec 25 2017 docker.service-> / lib/systemd/system/docker.servicelrwxrwxrwx 1 root root 35 Nov 29 2017 hciuart.service-> / lib/systemd/system/hciuart.servicelrwxrwxrwx 1 root root 38 Nov 29 2017 networking.service-> / lib/systemd/system/networking.servicelrwxrwxrwx 1 root root 37 Nov 29 2017 nfs-client.target-> / lib/systemd/system/nfs-client.targetlrwxrwxrwx 1 root root 48 Nov 29 2017 Raspberrypi-net-mods.service-> / lib/systemd/system/raspberrypi-net-mods.servicelrwxrwxrwx 1 root root 36 Nov 29 2017 remote-fs.target-> / lib/systemd/system/remote-fs.targetlrwxrwxrwx 1 root root 33 Nov 29 2017 rsync.service-> / lib/systemd/system/rsync.servicelrwxrwxrwx 1 root root 35 Nov 29 2017 rsyslog.service-> / lib/systemd/system/rsyslog.servicelrwxrwxrwx 1 root root 40 Apr 12 15:43 sshp.service-> / home/pi/openthings/tutools/sshp.servicelrwxrwxrwx 1 Root root 31 Apr 12 12:40 ssh.service-> / lib/systemd/system/ssh.servicelrwxrwxrwx 1 root root 37 Nov 29 2017 sshswitch.service-> / lib/systemd/system/sshswitch.servicelrwxrwxrwx 1 root root 40 Nov 29 2017 triggerhappy.service-> / lib/systemd/system/triggerhappy.servicelrwxrwxrwx 1 root root 47 Dec 25 2017 unattended-upgrades.service-> / lib/systemd/system/unattended-upgrades.servicelrwxrwxrwx 1 root root 40 Mar 24 2018 wifi-country.service-> / lib/systemd/system/wifi-country.servicelrwxrwxrwx 1 Root root 42 Apr 12 05:46 wpa_supplicant.service-> / lib/systemd/system/wpa_supplicant.service3.5 testing service starts and stops

Use the following command to control the service.

# View the service running status. Sudo systemctl status# starts the service. Sudo systemctl start#, you can then use ps-aux to see if the ssh process has been started. # stop the service. Sudo systemctl stop#, you can then use ps-aux to see if the ssh process has been shut down.

Here you have set a log file to sshp.log in the above directory, and you can view the output information.

You can modify the above script to add your own log information output.

4. Docker installation

You can also run tasks in Docker. I am here to create a Nginx service and then run the pac information provider to provide network configuration parameters for devices such as iPhone.

Get the docker automatic installation script and execute:

Wget https://get.docker.io-O docker.shsudo chmod docker.sh./docker.sh

Currently, the latest version of Docker is 18.09, but there is Bug at run time, which has not been solved yet.

We use version 18.06.1. Install the specified version of Docker:

Sudo apt install docker-ce=18.06.1-ce

Lock the version to avoid automatically upgrading to the latest version when you apt upgrade, as follows:

Sudo echo "docker-ce hold" | sudo dpkg-- set-selections

Test Docker to see if the installation is successful:

What is the docker info5 building PAC service pac?

Pac is a network configuration mechanism. The client automatically acquires a series of configuration parameters by obtaining the pac file, thus initializing the network.

Write a pac file as follows:

Function FindProxyForURL (url, host) {return "SOCKS 192.168.199.190pur5555";} pac service construction

Then, put the pac file into the Web server and build a file service (which can also be generated dynamically).

Nginx's Docker image is used here to provide services (note that Nginx has a variety of Docker images, and only those compiled for raspberry pie can run on it).

Run:

Docker run-- name nginx-v / home/pi/openthings/web:/usr/share/nginx/www:ro-d-p 80:80 akkerman/rpi-nginx

After the installation instructions are run, the written files are not released, because the default path for Nginx is / var/www/html, not / usr/share/nginx/www. You can modify the above command to:

Docker run-- name nginx-v / home/pi/openthings/web:/var/www/html:ro-d-p 80:80 akkerman/rpi-nginx

Or execute Exec to enter Docker to modify, as follows:

Docker exec-it nginx / bin/sh

View the service directory of Nginx:

Cat / etc/nginx/sites-available/default

Establish a soft connection:

Ln-s / usr/share/nginx/www / var/www/html

In the directory, create an index.html file, and then enter http://my-raspberrypi-ip in the browser to see if it can be published.

Then, put the above pac file in the release directory.

6. Test usability

Now, you can test whether the service established above is available.

Use the browser

Open the setting page, enter the network settings, check sokcs5, fill in the above pac address, check dns over https and other options.

Enter any address you want to access and wait for a miracle.

Use iPhone

Enter setting and select the network settings. There is an "automatic" option in it. Enter the pac address above.

7. Summary

There are some difficulties in writing a Linux service, which provides a minimalist framework.

Although the raspberry pie is small, it is enough to run some controlled services. The key is that the power consumption is low enough and there is no noise.

Some interesting tasks performed on a regular basis, such as time-shrinking photography, temperature and humidity monitoring, automatic watering, and so on, can be accomplished using the above mechanism.

The above is the minimalist Service written by raspberry pie using Shell. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report