關於 crontab 排程無法執行 [論壇 - Ubuntu基本設定]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



關於 crontab 排程無法執行
會員一級
註冊日期:
2017/6/4 9:56
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 157
EXP: 34
離線
請問一下各位前輩

12 12 * * * cvcl --- test.mp3

--- 代表正確的路徑,在文字界面下執行完全正常,但是把它放在排程裡面就無法執行了,作業系統是 debain,

已經試過了如下設定:

1. Crontab 起動

2022/10/18 10:00
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
版主
註冊日期:
2008/7/14 0:03
來自 螢幕的另一端
所屬群組:
網站管理員
已註冊使用者
討論區管理群
等級: 33
HP : 0 / 809
MP : 513 / 35951
EXP: 39
離線
因為你是撥音樂,所以在 crontab 裡,通常都是背景執行,那個背景執行可能就沒有音效系統或相關的驅動。 所以你要把它帶到你目前的環境去執行才行

範例:
vim /etc/crontab

55 11 * * * 使用者帳號 DISPLAY=:0 mpv --no-resume-playback /home/user/test.mp3

那個 Display=:0 是假設你只有一個桌面環境,單螢幕,若你不只一個螢幕的話,數字就要調整了。

2022/10/18 13:38
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
會員一級
註冊日期:
2017/6/4 9:56
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 8
MP : 1 / 157
EXP: 34
離線
不好意思 測試如下,還是無法執行

24 21 * * * sunrise display=:0 mpv --no-resume-playback cvlc /home/sunrise/Desktop/music/phoning1.mp3

2022/10/21 21:25
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
版主
註冊日期:
2008/7/14 0:03
來自 螢幕的另一端
所屬群組:
網站管理員
已註冊使用者
討論區管理群
等級: 33
HP : 0 / 809
MP : 513 / 35951
EXP: 39
離線
你那指令輸錯了 你都不知道
那指令要整個 copy 貼上終端機都能正常執行,你再寫入排程裡才有用
還有 display 大小寫也有差別,我給你的是全大寫的,你怎麼變全小寫了呢?

mpv 你不知是什麼意思的話,你就全拿掉吧

2022/10/22 0:15
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30224
EXP: 23
離線
joe99 寫到:
請問一下各位前輩

12 12 * * * cvcl --- test.mp3

--- 代表正確的路徑,在文字界面下執行完全正常,但是把它放在排程裡面就無法執行了,作業系統是 debain,

已經試過了如下設定:

1. Crontab 起動



樓主您在「#1」,用的播放指令是「cvcl」<-- 我猜這個應該是拼錯了



joe99 寫到:
不好意思 測試如下,還是無法執行

24 21 * * * sunrise display=:0 mpv --no-resume-playback cvlc /home/sunrise/Desktop/music/phoning1.mp3



樓主您在「#3」,用的播放指令是「cvlc」<-- 我猜您想要使用的應該是這個指令。


所以我猜您原本想要應該是如下的設定吧


24 21 * * * sunrise cvlc /home/sunrise/Desktop/music/phoning1.mp3




假設:
* 「sunrise」應該是您的帳號
* 「cvlc」應該是您想要使用的播放器
* 「/home/sunrise/Desktop/music/phoning1.mp3」應該是您想要播放的檔案路徑


## 參考文章

* G. T. Wang / Linux 設定 crontab 例行性工作排程教學與範例
* 鳥哥私房菜 / 第十五章、例行性工作排程(crontab)

================================================================================

## Debian Manpage

* Manpages of vlc-bin in Debian testing
* man cvlc


================================================================================

## 簡易探索

執行


which cvlc



顯示


/usr/bin/cvlc




================================================================================

執行


dpkg -S /usr/bin/cvlc



顯示


vlc-bin: /usr/bin/cvlc



表示「/usr/bin/cvlc」是來自於「Package: vlc-bin

================================================================================

執行


file /usr/bin/cvlc



顯示


/usr/bin/cvlc: POSIX shell script, ASCII text executable



執行下面指令,觀看「/usr/bin/cvlc」的內容


cat /usr/bin/cvlc



顯示


#! /bin/sh
exec /usr/bin/vlc -I "dummy" "$@"



也就是「cvlc」其實也是呼叫「vlc」,只是多了「-I "dummy"」這個參數。

註:

使用「man vlc」找不到「-I」的說明,可以執行「vlc -h」找到

================================================================================

以上提供參考


2022/10/22 1:24
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30224
EXP: 23
離線
## 補充一

另外您也可以把播放的這一段拉出來,寫成一個單獨的腳本,

爾後您只要先測試這個腳本是否能正常播放,

另外再測試您的「crontab」設定,

您的「crontab」設定就可以改成


24 21 * * * sunrise /home/sunrise/.local/bin/play-sound-phoning.sh



或是可以改成


24 21 * * * sunrise play-sound-phoning.sh




================================================================================

假設您的腳本放在「/home/sunrise/.local/bin/play-sound-phoning.sh」這個路徑

內容如下


#!/usr/bin/env bash

cvlc /home/sunrise/Desktop/music/phoning1.mp3





================================================================================

然後執行下面指令,將「/home/sunrise/.local/bin/play-sound-phoning.sh」設為可執行



chmod 755 /home/sunrise/.local/bin/play-sound-phoning.sh



或是執行下面指令,將「/home/sunrise/.local/bin/play-sound-phoning.sh」設為可執行


chmod u+x /home/sunrise/.local/bin/play-sound-phoning.sh



或是執行下面指令,將「/home/sunrise/.local/bin/play-sound-phoning.sh」設為可執行


chmod +x /home/sunrise/.local/bin/play-sound-phoning.sh




================================================================================

您就可以執行下面指令,測試「/home/sunrise/.local/bin/play-sound-phoning.sh」這個腳本



/home/sunrise/.local/bin/play-sound-phoning.sh



或是執行下面指令,測試「play-sound-phoning.sh」這個腳本。(假設您的「/home/sunrise/.local/bin/」有在『PATH」列表裡)


play-sound-phoning.sh



註:

* 關於「PATH」的概念,請參考「鳥哥私房菜 / 6.1.3 關於執行檔路徑的變數: $PATH
* 要檢查「~/.local/bin/」是否有在『PATH」列表裡,可以執行「echo $PATH」或是「env | grep '^PATH='」
* 這個設定在「Debian」系列,應該是在「~/.profile」
執行「cat ~/.profile」應該可以看到類似如下的內容


# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
	# include .bashrc if it exists
	if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
	fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
	PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
	PATH="$HOME/.local/bin:$PATH"
fi




================================================================================

2022/10/22 1:50
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30224
EXP: 23
離線
## 補充二

================================================================================


執行


apt-cache search mp3



或是執行


apt-cache search mp3 | less



或是執行


apt-cache search mp3 | grep player



應該可以看到其中部份內容如下



...略...
mpg123 - MPEG layer 1/2/3 audio player
mpg321 - Simple and lightweight command line MP3 player
...略...




================================================================================

## Debian Package

* mpg123
* mpg321

## Wiki

* Mpg123
* Mpg321

## Website

* mpg123
* mpg321

================================================================================

執行下面指令,觀看「Package: mpg321」的相關資訊


apt-cache show mpg321



顯示


Package: mpg321
Architecture: amd64
Version: 0.3.2-3.1ubuntu1
Priority: optional
Section: universe/sound
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Nanakos Chrysostomos <nanakos@wired-net.gr>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 123
Provides: mp3-decoder, mpg123
Depends: libao4 (>= 1.1.0), libasound2 (>= 1.0.23-2.1), libc6 (>= 2.14), libid3tag0 (>= 0.15.1b), libmad0 (>= 0.15.1b-3)
Recommends: libaudio-scrobbler-perl
Filename: pool/universe/m/mpg321/mpg321_0.3.2-3.1ubuntu1_amd64.deb
Size: 43036
MD5sum: 3c74d8320cf25a6b322d2c1d472a3e43
SHA1: 4374118c930d9936671af90b91da25592e7fd840
SHA256: 97871734f7c8a29e9e306e4d31d0d16d8a7cf3791efadf01e7fe76c600213fb8
SHA512: c606a40fae3559dbdd7c5a7530822036765463a69c921d10d0b0e9839bea61e722636355244bc49b4a3f047f1de11198fea9747ef729b8d4df8695b830302f9a
Homepage: http://mpg321.sourceforge.net
Description-en: Simple and lightweight command line MP3 player
 mpg321 is a clone of the popular mpg123 command-line mp3 player. It should
 function as a drop-in replacement for mpg123 in many cases. While some of
 the functionality of mpg123 is not yet implemented, mpg321 should function
 properly in most cases for most people, such as for frontends such as
 gqmpeg.
 .
 mpg321 is based on the mad MPEG audio decoding library. It therefore is
 highly accurate, and also uses only fixed-point calculation, making it
 more efficient on machines without a floating-point unit. It is not as
 fast as mpg123 on systems which have a floating point unit.
Description-md5: cf61479ef1985640113a399b6eef8973





================================================================================

執行下面指令,安裝「Package: mpg321


sudo apt-get install mpg321




================================================================================

執行


dpkg -L mpg321



顯示


/.
/usr
/usr/bin
/usr/bin/mpg321
/usr/share
/usr/share/doc
/usr/share/doc/mpg321
/usr/share/doc/mpg321/BUGS
/usr/share/doc/mpg321/HACKING
/usr/share/doc/mpg321/README.Debian
/usr/share/doc/mpg321/README.gz
/usr/share/doc/mpg321/README.remote
/usr/share/doc/mpg321/THANKS
/usr/share/doc/mpg321/TODO
/usr/share/doc/mpg321/changelog.Debian.gz
/usr/share/doc/mpg321/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/mpg321.1.gz



================================================================================


$ man mpg321


================================================================================

執行下面指令,播放「/home/sunrise/Desktop/music/phoning1.mp3」


mpg321 /home/sunrise/Desktop/music/phoning1.mp3



================================================================================


也可以執行下面指令,播放「/home/sunrise/Desktop/music/phoning1.mp3」


mpg123 /home/sunrise/Desktop/music/phoning1.mp3





關於「mpg123」是在安裝「Package: mpg321」過程中產生的

請參考「/var/lib/dpkg/info/mpg321.postinst」的內容


執行


dpkg -S /usr/bin/mpg123



顯示


dpkg-query: no path found matching pattern /usr/bin/mpg123



執行


file /usr/bin/mpg123




顯示


/usr/bin/mpg123: symbolic link to /etc/alternatives/mpg123



對照「「/var/lib/dpkg/info/mpg321.postinst」的內容,可以了解到是套用「Debian update-alternatives」的機制。

================================================================================


以上提供參考

主要是提供在「Debian系列」,如何簡易探索找出另一個「mp3播放器」。

2022/10/22 2:15
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30224
EXP: 23
離線
## 補充三

補充「#2」和「#4」提到的


可以執行「man mpv」,了解「mpv」這個指令的用法,
也可以找到「--no-resume-playback」這個參數的說明。


關於「Manpage」的用法,可以參考下面的文章

* G. T. Wang / 善用 man 指令查詢 Linux 線上手冊(Man Page)
* 鳥哥的 Linux 私房菜 / 4.3 Linux系統的線上求助man page與info page

2022/10/22 2:35
應用擴展 工具箱
回覆: 關於 crontab 排程無法執行
版主
註冊日期:
2008/7/14 0:03
來自 螢幕的另一端
所屬群組:
網站管理員
已註冊使用者
討論區管理群
等級: 33
HP : 0 / 809
MP : 513 / 35951
EXP: 39
離線
按我原來的設定方法,來設定 /etc/crontab 排程 來設定撥放鬧鐘的方式,在升級系統之後也不管用了,後來網路上有找到解法

https://askubuntu.com/questions/832072/can-i-use-cron-to-chime-at-top-of-hour-like-a-grandfather-clock/832266#832266

重點在於這行指令
export XDG_RUNTIME_DIR="/run/user/1000"
你得在撥放鬧鐘之前先執行過上面這行指令之後,再撥放鬧鐘才會有聲音

2/17 12:42:51
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.