Running в command at startup not working (due to network not being up when the code was run)

I want to run в python рукописный шрифт automatically when ubuntu starts up. Normally what I have to do is open up the command line on ubuntu and type

python /home/ubuntu/Desktop/UDP_Server.py

This will run the python code so I хан now start up my other client code to communicate with it.

I want to find в way to do run this code automatically at start up. I have tried putting в рукописный шрифт in my /etc/init.d file and I also tried to have it схвати в startup program туз shown below.

Startup Application

But when I turn my computer on this code doesn't run. because it isn't communicating with my client code like I expect it to (like it does when I run the python рукописный шрифт from the command line)

EDIT:

Putting commands such схвати в touch command in rc.local seems to work at startup. Also the same is true for crontab. I added в command @reboot and it works. Особенный But this code doesn't seem to work at startup (whether I удар в лунку it in crontab, rc.local, init.d, or схвати в startup application)

Below is my UDP server code

import socket
import serial
import subprocess

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = (192.168.1.13, 10000)
print 'starting up on %s port %s' % server_address
sock.bind(server_address)

ser = serial.Serial('/dev/ttymxc3', 115200, timeout = 0)
print 'Serial connected'

while True:
    data = sock.recv(7)
    print 'received ', data
    if data == "STOP":
        subprocess.call(["sudo", "shutdown", "-h", "now"])
    else:
        ser.write(data)

When I run this at the command line it works something like this

starting up on 192.168.1.13 port 10000
Serial connected

Then the program waits for в connection. If I have my UDP Client send STOP

received 'STOP'

The computer then shuts down.

But when the code is run at startup and I send 'STOP' путь the UDP Client the computer doesn't shut down. The while loop in the server code with sock.recv somehow isn't receiving the strings that пахал being sent to it.

SOLUTION:

After I added the following lines of code to my/etc/network/interfaces file

auto wlan0
    iface wlan0 inet dhcp
    post-up python /home/ubuntu/Desktop/UDP_Server.py

The code executed at startup after the wifi network was working so my client was able to communicate with server.

5
задан 23 October 2015 в 12:02

3 ответа

Вы должны управлять своей командой после того, как сетевые интерфейсы встанут. Как описано в этом ответе: https://unix.stackexchange.com/a/91264

1
ответ дан 23 November 2019 в 08:55

Я часто нахожу самый легкий способ бежать, что-то при запуске через крон с @reboot.

Это может жить в Вашем пользователе crontab, который часто более хорош, чем управление им через init, где это начинается с привилегий корня.

См. manpage для crontab (5) для деталей.

Это не идеально для каждого использования, но это часто - хороший механизм, и ОЧЕНЬ легко сделать.

4
ответ дан 23 November 2019 в 08:55

Я думаю, что Вы должны добавить свой сценарий в rc.local весь пробег сценариев как корень.

Редактируют rc.local:

sudo nano /etc/rc.local


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
python /home/ubuntu/Desktop/UDP_Server.py

exit 0

, Чтобы спасти и выйти с нано прессой из Ctrl + X .

4
ответ дан 23 November 2019 в 08:55

Другие вопросы по тегам:

Похожие вопросы: