1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

シェルのCGIでバックグラウンド起動する方法

1
Last updated at Posted at 2021-06-22

はじめに

別端末のウェブブラウザからCGI経由でラズパイ上のコマンドをバックグラウンド起動させたかったんですが、1日以上かかってしまったので、本当に簡単な話なんですが、メモっておきます。

使用環境

ハード
Raspberry Pi 4 RAM 4GB
OS
$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster
$ uname -a
Linux raspberrypi 5.10.42-v7l+ #1422 SMP Tue Jun 8 13:03:05 BST 2021 armv7l GNU/Linux
Apache
``` $ apachectl -v Server version: Apache/2.4.38 (Raspbian) Server built: 2020-08-25T20:08:29 ```

Apacheインストール後の各種設定

仮想ホストの設定ファイルを以下のように編集する。同フォルダに元ネタがある。

/etc/apache2/sites-available/xxx.conf
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
        AddHandler cgi-script .cgi .py

        <Directory "/var/www/cgi-bin">
                AllowOverride None
                Require all granted
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

以前の設定ファイルを無効化する

$ a2dissite yyy.conf

新しい設定ファイルを有効化する

$ a2ensite xxx.conf

設定をリロードする

# systemctl reload apache2

うまくいかなければ、リスタートするかシステムをrebootする。

例えば以下のような感じでシェルスクリプトファイルを置く。

/var/www/cgi-bin/xxx.cgi
# !/bin/sh


echo Content-Type: text/html; charset=UTF-8
echo

cat <<EOT
<html>
<body>Start Background Process
</body>
</html>
EOT
exec >&-
exec 2>&-

バックグランド起動したいコマンド コマンドライン引数等  &

※「exec」の2行がポイント(ただし、コメント必読)
※実行属性をつけておくこと。(sudo chmod +x xxx.cgi とか)

実行

同一LANの別端末のウェブブラウザから http://xxx.xxx.xxx.xxx/cgi-bin/xxx.cgi にアクセス

参考URL

Apache2.4の設定めも
Run a cgi shell script in background

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?