Ubuntu mate self-start script/command + close graphical desktop

Ubuntu mate self-starting script/command

There are three methods for Ubuntu mate self-starting scripts/commands, namely /etc/init.d method, /gnome-terminal method, and rc.local method. The
following will introduce them one by one and show the results.

hardware

I am using Raspberry Pi 4B, and the installed system is ubuntu mate 20.04 desktop

/etc/init.d method (failed)

First, let’s briefly introduce this file. Please refer to what folder /etc/init.d is :
/etc/init.d is a soft link pointing to /etc/rc.d/init.d. And this thing is only available under fedora. There is no /etc/rc.d/ directory under ubuntu, only /etc/init.d. There is /etc/rc.d under openSUSE, which is directly linked to /etc/init.d.
And sometimes you can see directories such as rc0.d/. Check the files in these directories and run ls -l to find that they are all soft links.

To understand the relationship here, you need to understand the run level of Linux. Open /etc/inittab, there should be comments in it,

# 0 – 停机(千万别把initdefault设置为0,否则系统永远无法启动)
# 1 – 单用户模式
# 2 – 多用户,没有 NFS
# 3 – 完全多用户模式(标准的运行级)
# 4 – 系统保留的
# 5 – X11 (x window)
# 6 – 重新启动 (千万不要把initdefault 设置为6,否则将一直在重启 )

Different run levels will start different services when booting up, so when init is executed after booting up (pdi is 1, it is the first process executed after booting up, all other processes are the descendants of this process), this process is Different default services will be opened according to different run levels (under fedora, rc.sysinit is read).

You can see that scripts in directories such as rc0.d/ are preceded by S or K, where s means start and k means kill. For example, when you switch from run level 5 to 3, some services may be turned off, and the script starting with k will be executed.

And the letters will be followed by numbers, which represent the order in which the scripts are executed. The smaller the number, the earlier it is executed. Many times, these execution orders are very important. For example, to start the Apache service, you must first configure the network interface, otherwise there will be no IP. Wouldn't it be funny to start the http service on your machine...

To sum up, the /etc/init.d directory is the script for all services. You can turn on, off or restart the corresponding services by executing the script and adding parameters. The rc.d/ directory should be turned on by default when the computer is turned on. services, and different directories such as rc0.d/ are divided according to different operations of the system. Directories such as rc0.d/ under ubuntu are placed directly under /etc/.

After talking about the role of this folder, we can then put our own script under this file and add it to the startup file. The specific method is as follows: (Take a script to start at boot as an example)

//1.首先要自定义一个脚本,比如自己创建一个脚本
sudo touch startshell.sh

//2.然后编辑这个脚本
sudo nano startshell.sh

//3.比如脚本内容如下:(第一行是要添加的解释器,后面是要执行的指令内容)
#!/bin/bash  //如果这个解释器不行  就换成这个  #!/bin/sh
echo "hello world" > /usr/local/test.log
exit 0

//4.编写了脚本之后,将脚本文件移动到/etc/init.d/文件夹中并添加权限
sudo mv startshell.sh /etc/init.d/
cd /etc/init.d/
sudo chmod 777 startshell.sh

//5.添加开机启动
sudo update-rc.d startshell.sh defaults 90 //后边的这个90是优先级,数越大,执行的越晚

//删除开机启动
sudo update-rc.d -f startshell.sh remove

At this point, the whole process is over, and then sudo reboot to restart and see if there is a self-starting program (here, I see that many scripts in the init.d folder do not have the .sh suffix, but they actually seem to create files It’s okay if you don’t add .sh, then don’t add .sh to all subsequent commands that involve this file)

Let me talk about my test results. The result was that it was not successful. I don’t know what went wrong. What this script says is to write hello world to test.log. Just open the system and check if there is such a file. It is true on the Internet. Many people said that this method is possible, but I had no success until I used the third method rc.local and noticed a problem: whether there was something wrong with my own script, that is, there was something wrong with the interpreter of the script or there was something wrong with the writing. Or execute the script in a terminal first to see if each sentence is executed. My script was executed in the terminal I opened, and there was no problem, but it still didn't work when I put it in the init.d folder and started it automatically.

Another important issue is that, for example, if you execute this script in your own terminal, there will be no problem, but there will be problems when you start the computer and execute it. One problem is that there is no source. My personal understanding is that the terminal is different from the terminal I opened. It’s not the same. Every time you open the terminal you open, source ~/.bashrc will be executed in advance. However, this terminal does not seem to have it, so you need to source it yourself and add the source statement to your own script, for example:

#! /bin/bash

source /opt/ros/noetic/setup.bash
source /home/tymiao/wsh/catkin_ws/devel/setup.bash

gnome-terminal -- roscore;
sleep 2s
gnome-terminal -- sudo chmod 777 /dev/ttyAMA2;sudo chmod 777 /dev/mem;sudo chmod 777 /dev/gpiomem;sudo chmod 777 /dev/ttyAMA0;sudo chmod 777 /dev/ttyAMA1;sudo chmod 777 /dev/ttyAMA3;sudo pigpiod;
sleep 2s
gnome-terminal -- roslaunch teleop_twist_joy teleop.launch;
sleep 2s
gnome-terminal -- rosrun learning_communication client_joy;
sleep 2s
gnome-terminal -- rosrun learning_communication server;

Another point is that I want to start my own program after turning on the computer without turning on the screen (I have set up the Raspberry Pi to still start without connecting to the monitor, please refer to the method of Raspberry Pi (Raspberry Pi) failing to start without connecting to the monitor ) , and close the system interface at the same time. If you want to control it, use ssh control.

If there is a program in your script that requires you to enter a password, then we will set it up to avoid entering the password when executing the command, otherwise it will be meaningless to start automatically. For specific steps, please refer to Ubuntu to avoid entering the password when executing the sudo command .

Everyone try this method. If it succeeds, please let me know in the comment area. Thank you very much.

gnome-terminal method (semi-successful)

Start ubuntu application preference management:

gnome-session-properties

If the above command is not executed successfully, just follow the prompts sudo apt-get install to install it. Just
Insert image description here
write the name as you like and the comments as you like. For commands, if you are executing a script, just click Browse at the back to select the script. The absolute path of the script will be displayed here if you want to execute the command. Then just write the command directly. Then click Add, and finally click Close in the lower right corner ( Key point: When you don’t want it to start automatically, just canceling the check mark in front of the startup program you added will not work. After restarting, it will still start automatically. You can only delete it. to cancel self-starting )

If there is a program in your script that requires you to enter a password, then we will set it up to avoid entering the password when executing the command, otherwise it will be meaningless to start automatically. For specific steps, please refer to Ubuntu to avoid entering the password when executing the sudo command .

Let me talk about the results of this method. The result is that it is successful and can start automatically. However, there is a problem, and this problem is not small. That is, when I don’t connect the monitor, it will not start automatically, or if I don’t connect the monitor, it can start automatically. Enter the system, but my program will not start automatically. It will start automatically when I use nomachine to open the system interface. This means that as long as I don't open the system's graphical interface, it will not start automatically. It will only start automatically when I open the graphical interface (no matter how I open the interface, connect a monitor or use nomachine). This is very strange and does not meet my requirements. , I have to find another way. If any of you have solved this problem, please let me know. Thank you! ! !

rc.local method (success)

The problem I encountered before running this method is that there is no such file. The path of this file should be in /etc/rc.local, but I don’t have it, so I referred to Ubuntu /etc/rc.local does not exist , Ubuntu 20.04 manual implementation rc.local has completed the creation of rc.local, and I have the rc-local.service file. There are three ways to set the startup command/script in ubuntu18.04 (sudo is available). Method three here is also possible. In short, you can create the /etc/rc.loacl file, give the rc.local file execution permissions and start the service. .

The rc.local script will be automatically executed after Ubuntu is booted, so we can uninstall this file with the startup command, for example:

#!/bin/sh -e
#
# rc.local
#
# ……
#

cd /home/User/Desktop/ && ./test.sh

exit 0

Note: Root permission is required when editing. If the method does not start correctly, check the log content. One thing to note: the script in rc.local will be executed before the system logs in. This is because we have not yet entered the desktop, so the execution may fail. Maybe it's because I didn't enter the desktop, so the system can start the program automatically without opening the desktop, which meets my requirements.

But when I add a script, such as the above, it will fail and cannot start automatically. After checking, I found that it is a problem with the terminal. The way to check is to add this sentence before exit0: echo "hello world" > /usr/local/test.log. Note that it is after your own script, because if you find it in /usr/local/test.log, you will print it. content, indicating that rc.local was executed but there was some problem with the test.sh script.
For example:

#!/bin/sh -e
#
# rc.local
#
# ……
#

cd /home/User/Desktop/ && ./test.sh
echo "hello world" > /usr/local/test.log
exit 0

After checking, I discovered the problem. Referring to the command not executing in rc.local in Ubuntu , it is said to be a problem with the shell. Some commands need to be executed in the bash shell and cannot be run in the dash.

solution:

1. Change the /etc/rc.local command to a more compatible mode, and change "#!/bin/sh" to "#!/bin/bash"

2. Relink /bin/sh to /bin/bash as follows:

Method 1: Execute sudo dpkg-reconfigure dash in the terminal, and then select no.

Method 2: Re-create the soft link and execute the following command:

sudo rm /bin/sh
sudo ln -s /bin/bash /bin/sh

There are also 5. Solutions for Raspberry Pi program self-starting and Python script self-starting (the reason why rc.local does not start automatically).
In this way, /bin/sh is linked to /bin/bash. After rebooting the system, the command executes normally.

Also remember to use sudo chmod +x script to add permissions to the script.

But I was really bad at it, and it still failed to run (you may succeed), so I had to put the contents of the script into rc.local myself. Specifically, I ran several ros nodes and then wrote several packages. into a launch file and directly added the command roslaunch xx xxx to rc.loacl and then solved my problem.

This is my successful /etc/rc.local

#!/bin/bash

source /opt/ros/noetic/setup.bash
source /home/tymiao/wsh/catkin_ws/devel/setup.bash

#roscore
sudo chmod 777 /dev/ttyAMA2
sudo chmod 777 /dev/mem
sudo chmod 777 /dev/gpiomem
sudo chmod 777 /dev/ttyAMA0
sudo chmod 777 /dev/ttyAMA1
sudo chmod 777 /dev/ttyAMA3
sudo pigpiod

roslaunch nlink_parser move.launch


echo "hello 301" > /usr/local/test.log

exit 0

Raspberry Pi ubuntu mate close image desktop

The resources of the Raspberry Pi are very limited, so turning off the graphical interface can save some resources and reduce power consumption. Therefore, I have two methods to turn off the system graphical interface: one is to connect the graphical interface through the ubuntu system , the second is to close the interface through raspiberry-configer. After closing the interface, we can use ssh control.

Ubuntu system closes the image interface

Close the graphical user interface

sudo systemctl set-default multi-user.target
sudo reboot

Enable graphical user interface

sudo systemctl set-default graphical.target
sudo reboot

Note: After closing the graphical interface, be sure to connect the monitor to the Raspberry Pi to check whether the system interface is closed. Do not use nomachine remote control software to check. After my test, no matter what method is used to close the graphical interface, there will be a graphical interface after using nomachine to connect. . But the fact is that it is really closed

Turn off the graphical interface through raspi-config

// 打开boot设置界面
sudo raspi-config

Insert image description here
Select the 3rd Boot Options
Insert image description here
select B1 Desktop/CLI
Insert image description here

There are four options here. The top two have no graphical interface, and the bottom two have graphics. We select "B2 Console Autologin" to not require a graphical interface and log in as the pi user by default (there is no need to manually enter the username and password every time you enter) , press Enter.

Exit (press ESC) the setting interface and restart the Raspberry Pi with sudo reboot.
By connecting to the monitor, the image interface is indeed closed. The displayed screen is the Ubuntu Mate logo that keeps rotating. It seems that you are entering the system, but in fact you have already entered the system and this interface will always be displayed. However, there will still be a graphical interface after nomachine is connected.

Okay, that’s it for this summary, keep working hard.

reference

1. The commands in rc.local in Ubuntu are not executed
2. Ubuntu /etc/rc.local does not exist
3. Ubuntu 20.04 manually implements rc.local
4. Ubuntu sets the boot execution script through Update-rc.d or rc.local
5 . Three methods for setting the boot startup command/script in ubuntu18.04 (sudo is supported)
6. The boot self-start script in the /etc/init.d directory of the linux system
7. Several options for Ubuntu’s boot self-start script/command
8. ubuntu automatically executes the script at boot
9. ubuntu20.04.3 closes the desktop correctly
10. ubuntu20.04 closes/opens the graphical interface
11. ubuntu starts automatically when booting and starts the ROS node
12. notes on the ros program that automatically starts at boot under Ubuntu (including rosrun and roslaunch )
13. Ubuntu starts the ROS node automatically at boot
14. Ubuntu Mate starts ROS automatically at
boot 15. Ubuntu18.04 closes and starts the graphical interface
16.Ubuntu does not need to enter the password when executing the sudo command
17. Ubuntu Mate starts ROS automatically after booting
18. What is the etc/init.d directory
19. 5. The Raspberry Pi program starts automatically and the Python script starts automatically (the reason why rc.local does not start automatically)
20. Raspberry Pi-Close the visual interface

Guess you like

Origin blog.csdn.net/weixin_41756645/article/details/126935963