Python実用:デスクトップ壁紙を毎日自動的に必ずトップページの画像に更新させる
7544 ワード
本編は目的を達成できるが、踏み込みが多く、更新後の文章を直接見ることができる.
シナリオ
デスクトップの壁紙が単調すぎて、適当な壁紙の出所がなくて、手動で更新するのは面倒だと感じています...発見するのは難しくなくて、毎日必ず対応するトップページのピクチャーはすべて更新して、その上ピクチャーは直接壁紙に持つことができます.だから、手を動かすと上の問題を解決することができます.
構想必須トップページ画像(python を取得する.はローカルファイル として保存する.ファイル自動ネーミング:当日の日付に基づいて(python コマンドで画像を壁紙(ubuntu環境、gnomeデスクトップ、python に設定コードを毎日1回走らせる( コード実装
次に、コードが正しく実行されるかどうかをテストします.
コマンドラインで直接実行するこのコードは問題なく、画像は正常にダウンロードして保存することができる.壁紙は正常に交換されています
ただし、このプログラムを毎日自分で実行することはできないでしょうから、
しかし最后にやっと発见して、
具体的な動作:スクリプトに実行権限を付与 スクリプトを 配置
スクリプトをcronに配置します.dailyでは実行されていません...次のいくつかの問題を確認しました.
(文書が仕様に適合しているかどうかを確認するコマンドこのディレクトリの下のすべての実行可能ファイルの所有者はrootでなければならないので、スクリプトの所有者(owner)をrootに変更します.これは、As described above,the files under these directories have to be pass some sanity checks including the following:be executable,be owned by root、 not be writable by group or other and, if symlinks, point to files owned by root. Additionally, the file names must conform to the filename requirements of run-parts: they must be entirely made up of letters, digits and can only con‐ tain the special signs underscores (’_’) and hyphens (’-’). Any file that does not conform to these requirements will not be executed by run-parts. For example, any file containing dots will be ignored. This is done to prevent cron from running any of the files that are left by the Debian package management system when handling files in/etc/cron.d/as configuration files (i.e. files ending in .dpkg-dist, .dpkg-orig, and .dpkg-new). が
壁紙の交換に失敗しました...cronを借りません.daily/了,直接crontabでループタスクを設定するスクリプトを に移動する. を呼び出す.は、
再度テストして、画像は正常にダウンロードすることができて、しかし壁紙はやはり交換していません最終的に1つのウェブサイトのコメントで問題点を見つけました: gsettings won’t work from cron, however. you need to set the DBUS_SESSION_BUS_ADDRESS environment >variable in order for gsettings to work. You can do that by adding these two lines before gsettings 上記のように、shellスクリプトに2つのshellコマンド(具体的な意味はよくわかりません) が追加されました.それから壁紙はやっと正常に毎日自動的に を交換することができます
シナリオ
デスクトップの壁紙が単調すぎて、適当な壁紙の出所がなくて、手動で更新するのは面倒だと感じています...発見するのは難しくなくて、毎日必ず対応するトップページのピクチャーはすべて更新して、その上ピクチャーは直接壁紙に持つことができます.だから、手を動かすと上の問題を解決することができます.
構想
requests
ライブラリ)time
ライブラリ)os
ライブラリ)crontab
コマンド)#!/usr/bin/python3
# -*- "encoding: utf-8" -*-
import requests
import time
import os
# ,
img_url = "https://area.sinaapp.com/bingImg/"
#
date = time.localtime()
year = date.tm_year
month = date.tm_mon
day = date.tm_mday
filename = "bing_%s_%s_%s.jpg" % (year, month, day)
#
r = requests.get(img_url)
with open("/home/ubuntu/Wallpapers/%s"%filename, "wb") as f:
f.write(r.content)
# shell
os.system("gsettings set org.gnome.desktop.background picture-uri 'file:///home/ubuntu/Wallpapers/%s'" % filename)
次に、コードが正しく実行されるかどうかをテストします.
コマンドラインで直接実行するこのコードは問題なく、画像は正常にダウンロードして保存することができる.壁紙は正常に交換されています
ubuntu@ubuntu-X550VQ:~$ ./autoUpdateWallpapers.py
ただし、このプログラムを毎日自分で実行することはできないでしょうから、
cron
とanacron
を利用してコードを毎日走らせておけばいいのですしかし最后にやっと発见して、
cron
はgsetting
に対して/etc/cron.daily
に対して友好的ではありません—>だからもし间违いを试みたくないならば振り回したくないならば、下をスキップしてプログラムをcronに入れてください.dailyディレクトリの方法具体的な動作:
chmod 755 autoUpdateWallpapers.py
cron.*/
ディレクトリの下にsudo mv ./autoUpdateWallpapers.py /etc/cron.daily/autoUpdateWallpapers
sudo chown root /etc/cron.daily/autoUpdateWallpapers
sudo chgrp root /etc/cron.daily/autoUpdateWallpapers
スクリプトをcronに配置します.dailyでは実行されていません...次のいくつかの問題を確認しました.
run-parts
ディレクトリの下に置かれたファイルはrun-parts --test /etc/cron.daily/
によって実行されるため、ファイル名にはドット(dots)などを持ち込めないなどの要求があるため、スクリプトをターゲットフォルダに移動した後、接尾辞を書かないでください.(文書が仕様に適合しているかどうかを確認するコマンド
run-parts
命令出力リストに適合しているかどうかを確認する)cron
文書を詳細に表示するcron.daily/
のフォルダに入れるファイルシステムは、毎日1回実行することを保証し、具体的な時間はcrontab
とanacron
の配置状況によって決定する.プロファイルはそれぞれ/etc/crontab
と/etc/anacrontab
である.壁紙の交換に失敗しました...cronを借りません.daily/了,直接crontabでループタスクを設定する
cron.daily/
フォルダから他のフォルダ、例えば/usr/bin/
ディレクトリの下$HOME
の下にshellスクリプトautoUpdateWallpapers
を新規作成し、shellスクリプトによってpythonプログラム#!/bin/sh
python3 /usr/bin/autoUpdateWallpapers.py
crontab -e
を使用してshellスクリプトを現在のユーザのタイミングタスクリストに書き込む、この時点で時間が明確に決定される.欠点は、実行時間になってコンピュータが正常に動作しないと、今回のタスクがスキップされることである.逆に、cron.*/
シリーズのフォルダの下に置くスクリプトは、anacron
によって時間間隔の確認が行われ、「補完」することができる.# crontab -e ,
0 22 * * * /home/ubuntu/autoUpdateWallpapers
再度テストして、画像は正常にダウンロードすることができて、しかし壁紙はやはり交換していません
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
– willbradley Jan 13 at 0:25 #!/bin/sh
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
python3 /usr/bin/autoUpdateWallpapers.py