Autostart does not work after user reboot

2019-07-24 03:40发布

问题:

I want to autostart a sh script after reboot.

I add below line to /etc/xdg/lxsession/LXDE-pi/autostart.

@/home/pi/mog/run.sh

Everything works fine if i plug the raspberrypi-2 to power line or switch on it after halt. However when i run shell command sudo shutdown -r now the autostart method does not work after reboot?

I want to run a C++ application after reboot and my run.sh script is below.

#!/bin/bash sleep 5 /home/pi/mog/mog -platform xcb

回答1:

I guess it depends on what type of shell script you are trying to run.

Looks like there are several ways to do it:

1. Start with Console

Create a file for your startup script and write your script in the file:

 $ sudo nano /etc/init.d/superscript
 Save and exit: Ctrl+X, Y, Enter

Make the script executable:

 $ sudo chmod 755 /etc/init.d/superscript

Register script to be run at startup:

 $ sudo update-rc.d superscript defaults

2. Start At Login

Make sure you are in the pi folder:

 $ cd ~

Create a file and write a script to run in the file:

 $ sudo nano superscript

Paste contents of sh script here

 Save and exit: Ctrl+X, Y, Enter

Open up .bashrc for configuration:

 $ sudo nano .bashrc

Scroll down to the bottom and add the line: ./superscript

 Save and exit: Ctrl+X, Y, Enter

3. Start with Desktop Environment

(Your method which sounds like its not working for your needs)



回答2:

I suggest to edit your crontab.

$crontab -e

or

$sudo crontab -e

The last line should be

@reboot /home/pi/mog/run.sh

If it doesn't work you can try creating an autostart folder

mkdir ~/.config/autostart
cd ~/.config/autostart

create a .desktop file like this:

[Desktop Entry]
Type=Application
Exec=yourscript.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Startup Script

Move your yourscript.sh in /usr/local/bin folder and it should run after reboot