シェルスクリプトを使用してファイルをsudoにコピーし、パラメータを渡す方法

シェルスクリプトを使用してファイルをsudoにコピーし、パラメータを渡す方法

シェルスクリプトを使用して仮想マシンを設定したいと思います。 script.shの例では

pip install wheel
pip install cookiecutter
pip install flask 
pip install gunicorn
pip install uwsgi

次に、/etc/systemd/system/website.serviceの場所に次の内容でサービスファイルを生成したいと思います。

[Unit]
Description=Gunicorn instance to serve website
After=network.target

[Service]
User=$1
Group=www-data
WorkingDirectory=/home/$1/website
Environment="PATH=/home/$1/website/venv/bin"
ExecStart=/home/$1/website/venv/bin/gunicorn --workers 3 --bind unix:website.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

ここで、$1 はシェルスクリプトを実行するユーザー ($USER) に置き換えられます。最善の解決策は、別のファイルに入れてからパラメータを置き換えるときに指定した場所にファイルをコピーすることです。重要なことは、場所のために貼り付けるためにsudoが必要であるということです。

それは次のとおりです。

pip install wheel
pip install cookiecutter
pip install flask 
pip install gunicorn
pip install uwsgi
sudo echo file_containing_text.txt $USER > /etc/systemd/system/website.service

しかし、私を愛しているので、私はそれを成功させることができませんでした。

ベストアンサー1

これを行うためのより良い方法があるかもしれませんが、実行したいアクションを具体的に指定するには、「ここにあるドキュメント」を使用できます。

#!/bin/bash
pip install wheel
pip install cookiecutter
pip install flask 
pip install gunicorn
pip install uwsgi
sudo cat > /etc/systemd/system/website.service << EOF
[Unit]
Description=Gunicorn instance to serve website
After=network.target

[Service]
User=${USER}
Group=www-data
WorkingDirectory=/home/${USER}/website
Environment="PATH=/home/${USER}/website/venv/bin"
ExecStart=/home/${USER}/website/venv/bin/gunicorn --workers 3 --bind unix:website.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target
EOF

<< TOKENと行の間のすべてはTOKEN文書です。私の例ではこれをEOFトークンとして使用しています。

おすすめ記事