質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

5338閲覧

raspiでsystemdを使っての自動実行ができない

nagog

総合スコア16

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

2クリップ

投稿2017/09/15 07:42

編集2017/09/17 23:32

###前提・実現したいこと
raspi3 でpython で書いたプログラムをOS起動時に自動実行したい。

###発生している問題・エラーメッセージ
OS起動時に自動実行されません。

/etc/systemd/system/test.service
上記ファイルを作成し、ファイルの内容は下記のとおりです。

[Unit] Description = Test [Service] ExecStart=/usr/bin/python /home/pi/nas_all/test013.py Restart=always Type=simple [Install] WantedBy=multi-user.target

その後、スタート、自動実行設定を行いました。

$ sudo systemctl start test.service $ sudo systemctl enable test.service Created symlink from /etc/systemd/system/multi-user.target.wants/test.service to /etc/systemd/system/test.service. systemctl list-unit-files --type=service

リストを見てもenableでした。

UNIT FILE STATE packagekit.service static pigpiod.service disabled plymouth-halt.service static plymouth-kexec.service static plymouth-log.service static plymouth-poweroff.service static plymouth-quit-wait.service static plymouth-quit.service static plymouth-read-write.service static plymouth-reboot.service static plymouth-start.service static plymouth-switch-root.service static plymouth.service static polkitd.service static procps.service static quotaon.service static rc-local.service static rc.local.service static reboot.service masked rescue.service static syslog.service enabled systemd-ask-password-console.service static systemd-ask-password-plymouth.service static systemd-ask-password-wall.service static systemd-backlight@.service static systemd-binfmt.service static systemd-fsck-root.service static systemd-fsck@.service static systemd-halt.service static systemd-hibernate.service static systemd-hostnamed.service static systemd-hybrid-sleep.service static systemd-initctl.service static systemd-journal-flush.service static systemd-journald.service static systemd-kexec.service static systemd-localed.service static systemd-logind.service static systemd-machined.service static systemd-modules-load.service static systemd-networkd-wait-online.service disabled systemd-networkd.service disabled systemd-nspawn@.service disabled systemd-poweroff.service static systemd-quotacheck.service static systemd-random-seed.service static systemd-readahead-collect.service disabled systemd-readahead-done.service static systemd-readahead-drop.service disabled systemd-readahead-replay.service disabled systemd-reboot.service static systemd-remount-fs.service static systemd-resolved.service disabled systemd-rfkill@.service static systemd-setup-dgram-qlen.service static systemd-shutdownd.service static systemd-suspend.service static systemd-sysctl.service static systemd-timedated.service static systemd-timesyncd.service disabled systemd-tmpfiles-clean.service static systemd-tmpfiles-setup-dev.service static systemd-tmpfiles-setup.service static systemd-udev-settle.service static systemd-udev-trigger.service static systemd-udevd.service static systemd-update-utmp-runlevel.service static systemd-update-utmp.service static systemd-user-sessions.service static test.service enabled test2.service enabled udev-finish.service static udev.service static

最初は動いていたのですが、stop,disnableや killall pythonを繰り返していたら
起動しなくなりました。
この後どのようにしたら自動実行できますでしょうか?
ご教示願います。

また、startで実行したときは実行できているのですが、プロセスで見れないのはなぜでしょうか?

pi@raspberrypi:~ $ ps u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND pi 827 0.0 0.4 6092 4472 tty1 S+ 16:20 0:00 -bash pi 2042 0.6 0.4 5960 4388 pts/0 Ss 16:43 0:00 /bin/bash pi 2071 0.0 0.2 4220 2116 pts/0 R+ 16:43 0:00 ps u

rc.localへの記述でも実行されませんでした。

sudo python /home/pi/nas_all/test013.py exit 0

###追加
追加確認事項です。
起動時に失敗しているようです。
理由がよくわかりません。

pi@raspberrypi:~ $ sudo systemctl status test.service ● test.service - Test Loaded: loaded (/etc/systemd/system/test.service; enabled) Active: failed (Result: start-limit) since 月 2017-09-18 08:19:26 JST; 39s ago Process: 718 ExecStart=/usr/bin/python /home/pi/nas_all/test013.py (code=exited, status=1/FAILURE) Main PID: 718 (code=exited, status=1/FAILURE) 9月 18 08:19:25 raspberrypi systemd[1]: test.service: main process exited,...E 9月 18 08:19:25 raspberrypi systemd[1]: Unit test.service entered failed s.... 9月 18 08:19:26 raspberrypi systemd[1]: test.service holdoff time over, sc.... 9月 18 08:19:26 raspberrypi systemd[1]: Stopping Test... 9月 18 08:19:26 raspberrypi systemd[1]: Starting Test... 9月 18 08:19:26 raspberrypi systemd[1]: test.service start request repeate.... 9月 18 08:19:26 raspberrypi systemd[1]: Failed to start Test. 9月 18 08:19:26 raspberrypi systemd[1]: Unit test.service entered failed s.... Hint: Some lines were ellipsized, use -l to show in full.

また、プロセスには表示されていませんでした。(文字数の関係で一部略)

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND pi 1226 0.0 0.1 2344 1444 ? S 08:19 0:00 gnome-pty-helpe pi 1227 0.1 0.4 5960 4384 pts/0 Ss 08:19 0:00 /bin/bash pi 1292 0.0 0.2 4220 2184 pts/0 R+ 08:22 0:00 ps aux

手動でスタートさせると確認できました。

pi@raspberrypi:~ $ sudo systemctl start test.service pi@raspberrypi:~ $ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1384 12.4 0.9 21288 8916 ? Ssl 08:24 0:00 /usr/bin/python

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

とりあえずsudo systemctl status test.serviceで状態を確認できますので確認してみてください。

また、startで実行したときは実行できているのですが、プロセスで見れないのはなぜでしょうか?

またpsx(=端末のないプロセスも表示)オプションもつけて確認してください。
典型的にはps auxをよく使います。

参考 : 【 ps 】 実行中のプロセスを表示する

syslogの見方など、以下の類似質問も参考になるかと思います。
自作のデーモン設定について(systemd)

投稿2017/09/15 08:09

can110

総合スコア38260

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nagog

2017/09/17 23:58

原因はよくわかりませんが、リスタートしたり、[Service]の中を改行してたら動くようになりました。 sudo systemctl restart test.service [Service] ExecStart=/usr/bin/python /home/pi/nas_all/test013.py Restart=always Type=simple
guest

0

自分宛の備忘録です。
$ sudo systemctl disable test.service
上記で一回切ってから、再度enableにするといいのかも・・・?
$ sudo systemctl enable test.service

投稿2017/09/18 05:36

nagog

総合スコア16

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問