I am a beginner and want to learn the method to run python script in linux system so that an application can automatically execute a job. For example, I want my ubuntu to do the cron job with a script to play a mp3 file by vlc player at 9pm every night , 7 audio files are named as Monday.mp3, Tuesday.mp3, Wednesday.mp3, Thursday.mp3, Friday.mp3, Saturday.mp3, Sunday.mp3 and are put in a folder as 7_Songs. In the script I expext to be like that : check date_weekday,load vlc player, play Monday.mp3 on Monday, play Tuesday.mp3 on Tuesday .... play Sunday.mp3 on Sunday.

Is it possible ? Can you show me any similar script or material for reference so that I can learn to write the script ? When the method is learnt, I guess I can further use them on open sourse application to do automation job.

Thank you very much for your help and guidance.

Dani AI

Generated

A short, practical plan for : pick the correct MP3 inside a small Python script, invoke VLC from the script in a way that does not rely on a desktop session, and schedule the script to run nightly at 21:00 with cron (or a systemd user timer). was on the right track suggesting a program call from Python; using the subprocess module and absolute paths makes that call robust. See the Python subprocess documentation for details: https://docs.python.org/3/library/subprocess.html.

Example approach (concept only):

Create an executable script that maps the current weekday to the matching filename, then launches VLC in command-line mode so it will run from cron. Test the script interactively first.

#!/usr/bin/env python3
from datetime import datetime
from pathlib import Path
import subprocess

base = Path('/home/USER/7_Songs')
names = ['Monday.mp3','Tuesday.mp3','Wednesday.mp3','Thursday.mp3','Friday.mp3','Saturday.mp3','Sunday.mp3']
today = datetime.today().weekday()   # 0 = Monday
file = base / names[today]
subprocess.run(['cvlc', '--play-and-exit', str(file)])

Schedule it at 21:00 with cron; include full paths and redirect output to a log for troubleshooting:

0 21 * * * /usr/bin/env python3 /home/USER/scripts/weekday_player.py >> /home/USER/logs/weekday_player.log 2>&1

Notes and common pitfalls: use cvlc or vlc --intf dummy so no GUI is required; cron provides a minimal environment so always use absolute paths and a shebang; verify playback manually before scheduling; if audio does not play under cron it is usually an environment or sound-session issue (systemd user timers can be easier for session-bound audio). See the Ubuntu cron howto and VLC command-line docs for more on scheduling and player options: https://help.ubuntu.com/community/CronHowto and https://wiki.videolan.org/VLC_command-line_help/.

Recommended Answers

All 2 Replies

You could try os.system(vlc 7_songs/Monday.mp3) , but I don't know if that's what you want, or even if that works that way.

thx Linux. I will try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.