Can a crontab jobs list transfer into an sh script?[SOLVED]

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
tompoe
Level 2
Level 2
Posts: 51
Joined: Sat Dec 03, 2011 5:58 am

Can a crontab jobs list transfer into an sh script?[SOLVED]

Post by tompoe »

I am a beginner that just went through a learning experience. I recently lost the chimes mechanism on my small grandfather clock. I pursued the goal of creating a crontab jobs project, and managed to fashion a replacement for the westminster chimes to mark hourly and 1/2 hourly ringing of the chimes and strikes marking the hour:
GNU nano 4.8 /tmp/crontab.QaSEmy/crontab
#!/bin/bash
XDG_RUNTIME_DIR="/run/user/29999"
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
[ Read 71 lines ]
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify
^X Exit ^R Read File ^\ Replace ^U Paste Text ^T To Spell

My level of imagination is incapable of how this effort might be rewritten as a script. Any pointers I might follow would be greatly appreciated. HAPPY HOLIDAYS!
Last edited by LockBot on Fri Jun 02, 2023 10:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Can a crontab jobs list transfer into an sh script?

Post by vimes666 »

Start by executing this command:

Code: Select all

man 5 crontab
If you think the issue is solved, edit your original post and add the word solved to the title.
mikeflan
Level 17
Level 17
Posts: 7158
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Can a crontab jobs list transfer into an sh script?

Post by mikeflan »

Lets get some facts clarified.
I pursued the goal of creating a crontab jobs project, and managed to fashion a replacement for the westminster chimes
That statement indicates you were successful in creating a crontab job that did the westminster chimes. Is that true?
tompoe
Level 2
Level 2
Posts: 51
Joined: Sat Dec 03, 2011 5:58 am

Re: Can a crontab jobs list transfer into an sh script?

Post by tompoe »

Hi, Mikeflan: Yes. Each hour the chimes sound, followed by the number of strikes matching the time. One o'clock occurs twice each day, so 12 one liners were used. When I read the man page, as suggested above, I began to realize I was, indeed, working with a "script". I'm just curious, whether someone out there recognizes this as a way to call the chimes script and strike script that gives the correct number of strikes each hour. I'm not sure which programming language would be best to tackle, but I'm thinking this example would be interesting to use as a learning project. For "clarity", know that I have way too much time on my hands. :oops:
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Can a crontab jobs list transfer into an sh script?

Post by vimes666 »

It can be done in crontab with 2 lines..
For the hourly chime you can use this:

Code: Select all

* 0 * * * for i in $(seq $(date +%h)); do mpv /path/to/your/bigben/audiofile; sleep 0.5s; done
and for the 30 minute chime you can this:

Code: Select all

30 * * * * mpv /path/to/your/30-minute-chimes/audiofile
If you want a 24 hour clock use %H instead of %h

(I did not test it) :)
If you think the issue is solved, edit your original post and add the word solved to the title.
tompoe
Level 2
Level 2
Posts: 51
Joined: Sat Dec 03, 2011 5:58 am

Re: Can a crontab jobs list transfer into an sh script?

Post by tompoe »

Hi, vimes666: Thanks much. I wish it were as elegant as you put it. I see right away, staying with shell scripting for scheduling tasks like my chimes thingy has a promising future. When one o'clock chimes, the strike count is one. When five o'clock chimes, the strike count is five. To me, that first line of code counts the hours, alright. However, I'm not sure one audio file is possible to increase the number of strikes for every hour. I think, if I create an audio file that has one strike, and an audio file that has the hourly chimes, it might be possible to have a one-liner that uses your seq command and adds another seq for the strike file. Thanks again for the suggestion. I might try it out, soon. In the meantime, I stumbled across some beginner tutorials with python. Do you use python?
mikeflan
Level 17
Level 17
Posts: 7158
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Can a crontab jobs list transfer into an sh script?

Post by mikeflan »

I am not vimes666, but I thought I would comment anyway.
I suggest you learn Python, and Rust, and maybe Ruby on Rails. Not all at once, but eventually. I hear Go is fairly easy to learn.
I know Perl only.
deepakdeshp
Level 20
Level 20
Posts: 12341
Joined: Sun Aug 09, 2015 10:00 am

Re: Can a crontab jobs list transfer into an sh script?

Post by deepakdeshp »

Perl the Swiss knife with many capabilities. But now not in news whereas Python is in the news.
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Can a crontab jobs list transfer into an sh script?

Post by vimes666 »

I understand you just want to do it with a distinct script or program. I agree using these sort of one-liners in crontab can be handy but is not very neat. However regardless of what programming language you are going to use, using crontab to trigger it would be the best way I think. It is designed for that purpose and it can be quite nasty to program yourself.
Regard the following as an example to try out in this manner.

1. create folder bin in your home directory if it is not there yet

Code: Select all

mkdir bin
2. create a program like following bash example with the name /home/your-username/bin/your-program

Code: Select all

xed bin/your-program
The program/script:

Code: Select all

#!/usr/bin/bash

play_hourly() {
   # loop from 1 until-including the current hour
   for i in $(seq $(date +%"$@")); do
      mpv /path/to/your/bell/sound
      sleep 0.5s
   done
}

play_30() {
   mpv /path/to/your/30min/sound
}

# $@ contains the parameter, H, I or 30
case "$@" in              
   [HI]) play_hourly "$@" ;;     # H=24hour I=12hour
   30) play_30 ;;
   *) echo "Invalid input"; exit 1 ;;
esac

exit 0
3. save it and make it executable

Code: Select all

chmod +x bin/yourprogram
4. add these lines to crontab:

Code: Select all

* 0 * * * /home/your-username/bin/your-program I
30 * * * * /home/your-username/bin/your-program 30
If you think the issue is solved, edit your original post and add the word solved to the title.
Locked

Return to “Beginner Questions”