date function in file which starts on system startup - bash - linux

I am trying to run a bash script on start up. The aim of the script is to play an mp3 file with the music player mplayer, at a set time (say at 15 past every hour).
My bash script is the following:
#!/bin/sh
while :
do
S=$(date)
T=${S:14:2} #this gives me the minute column of the current time
if [ $T -eq 15 ]
then
mplayer path_to_mp3_file
fi
done
When I run this bash file from the terminal it works absolutely fine.
However, when I restart my Linux computer with exactly the same script, it fails to work(this is also the case with other music players such as vlc). The script also works when it only contains the mplayer file_path command.
I have tried setting a standard program to open the script, the gnome-terminal.wrapper.
The way in which I was able to set the program as a start up application is in menu>Preferences>Startup applications and add the file to the already existing applications.

There are 2 parts to your problem. 1. play the mp3, 2. (presumably only when the user is logged in). For part 1, use a crontab and have the cron daemon run the script at 15 past the hour. You create your crontab file with crontab -e (to edit your crontab). The format of a crontab entry is:
* * * * * command_to_execute
| | | | |
| | | | +- day of week (0-6) (Sunday = 0)
| | | +--- month (1-12)
| | +----- day of month (1-31)
| +------- hour (0-23)
+--------- minute (0-59)
In your case you would want:
15 * * * * playmymp3.sh
Part 2 In your script you will want to check whether the user is logged in. To only play the music when you are logged in, test that you are logged in with users. Something like this in playmymp3.sh should work:
if grep -q "yourlogin" < <( users ); then
#play mp3 file
fi
Give it a try and let me know.

Try with:
T=$(date +%M)
instead of:
S=$(date)
T=${S:14:2} #this gives me the minute column of the current time
(note: your script enters in a very fast loop when no sound must be played, I suggest you include a sleep command).

Apologies on my part.
When setting the script as a startup file, I had not noticed that I had to provide an actual command, bash script_path, instead of solely the file path.
With this, my problem seems to be solved.

Related

crontab doesnt work in linux but manually works

I made a shell script to connect using ssh with password to another linux server, get the details of a specific file and save it in a log file in the origin server. Manually works without problem but with the /etc/crontab doesnt update the file. (I think it may be the connection because if I try to write a test text in the file it works fine).
I tried with tee -a command and the >> command to update the file and both fails.
This is my code
#! /bin/bash
sshpass -p "password" ssh "username"#"ipserver" ls -l /filepath/file.txt | tee /home/user/test/details.log
I omitted the password, username and ip for discretion.
This is the /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
* * * * * user-name command to be executed
*/1 * * * * root /home/test/script.sh
Hope you can help me.
Thanks
My first question is, why do you run your script as root? This opens up security holes if the script itself or the directory /home/test is not owned by root.
If you are running the script as root from crontab, have you tested it as root from the command line?
I see directories /home/test and /home/user/test in your post. Do they really both exist and why?
Imho, what you should do:
Setup public key authentication between your user test and the remote user on the system. Use ssh-keygen to generate a public/private key pair, then copy the public key onto the target system and save it to ~/.ssh/authorized_keys. This will eliminate you having to use sshpass and have the password in cleartext in your crontab file.
Test the script as user test and then install it in the user test's crontab file. Simply type crontab -e as user test and you can add the same entry there.

crontab: same script is triggered only on one day

I'm not a linux expert and need some support to a crontab mystery (for me).
I'd like to do a backup of my raspberry pi twice a week.
It's the same script. But only the every monday trigger (dow=1) executes.
The Friday rule (dow=5) does nothing at all - no backup saved.
I can't see why.
What's going wrong? Where can I find out what's going wrong?
# 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:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 4 * * 1 /home/pi/Backup/backup.sh > /dev/null
0 4 * * 5 /home/pi/Baclup/backup.sh > /dev/null
screenshot of crontab -e
You did everything correctly. You were just missing a second pair of eyes to see that you have a typo in the second line:
Baclup vs Backup
;-)

Subshells in Bash, using crontab

I am currently working on some school project where we should be dealing with cron jobs, basically, we are building a simple CLI to do CRUD operations using Bash.
I found this snippet of code that inserts a new job into my crontab, yet I have no idea how it works... I understand it uses subshell and pipes, yet I just don't know why I would have to do it
read job
{ crontab -l; echo "$job"; } | crontab -
crontab is a file which contains jobs (instructions) for cron daemon (time-based job scheduler for Unix operating system).
If you put your bash lines into a script , let's say cron_test.sh
#!/usr/bin/sh
read job
{ crontab -l; echo "$job"; } | crontab -
And afterwards if you execute the script ./cron_test.sh, you'll see that the scripts awaits from stdin your input (which stores it into variable named job) in order to create a new job for your user.
Be careful because you have to respect the job syntax:
1 2 3 4 5 /path/to/command arg1 arg2
where:
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command – Script or command name to schedule
Some commands:
crontab -l - list current crontab (for the user which you're using)
crontab -e - edit the crontab file

Linux script which checks users and their groups

I am completely new to the shell scripting. Please help me out in below requirement. Thanks
Let us think a,b,c,d are the part of group z.
I want to write a script which runs every day particular time (example 8:00 AM)
The script should look if the user is a part of group z, if he is a part of group z then run particular command (example whoami)
Please help out
Try this:
G='cdrom';
if [ `groups | grep cdrom | wc -l` -eq 1 ];
then whoami;
else echo "Not in group $G";
fi
This should print the user's username with "whoami" if the user is in the group "cdrom". If they're not in the group "cdrom" then it should print "Not in group cdrom". Then you can set it up as a cronjob by editing your crontab like 0 8 * * * /path/to/script with the command crontab -e which will make the program run at 8 am every day.
Please note that this has very basic functionality just so that you can get the idea. You'll have the edit the script to really do anything useful.

How do i activate cron command once within specific time frame?

Basic information about my system: I have a music system where people can schedule songs to start and end at a specific time.
OS: Arch linux
It sets two crons at the moment. One lets say at 1.50 (start time with a command like "play etc") and another set at 3.20 (end time with a command like "end etc").
My setup works perfectly and i can end delete schedules etc etc but i now noticed an issue! If i set the above times and turn the system off (My system is a raspberry pi) and turn back on at lets say 2.00 and i missed the 1.50 deadline, the music doesnt start (obviously) and i want to try make it so no matter what time i turn it on within a range lets say: 1.50 - 3.20 it will start the play command. But it will run the command once!
I looked around and the commands i got was like:
0 1.50-3.20/2 * * * your_command.sh
But thats to run every 2 hours. I want it to run once only between these times?
Thanks!
You could add an additional cron job which starts a script on every reboot. For instance, you could add a line like this to your crontab:
#reboot /home/pi/startplayback.sh
Your startplayback.sh script should check if current time is within the desired period and run the desired command if it is. For example the code below will print PLAY! if the script is run between 1:50 and 3:20. You could replace echo 'PLAY!' by run WHATEVER
#!/bin/bash
current=$(date '+%H%M')
(( current=(10#$current) ))
((current > 150 && current < 320 )) && echo 'PLAY!'
P.S. Don't forget to make your script executable sudo chmod +x startplayback.sh
You might want to look at the at command and its utilities.
SYNOPSIS
at [-q queue] [-f file] [-mldbv] time
at [-q queue] [-f file] [-mldbv] -t [[CC]YY]MMDDhhmm[.SS]
at -c job [job ...]
at -l [job ...]
at -l -q queue
at -r job [job ...]
atq [-q queue] [-v]
atrm job [job ...]
batch [-q queue] [-f file] [-mv] [time]
at is good for scheduling one time jobs to be run at some point in the future. It maintains a queue of these jobs, so you can use it to schedule things with a great variety of different time specifications.
Cron is in my opinion a scheduler for jobs that are to be repeated over and over.
So a quick and dirty example for you:
echo 'ls -lathF' | at now + 1 minute
As expected you will see a job to be run in one minute. Try atq to see the list of jobs.
When the job is done, output will be mailed to your user by default.
I solved the issue by creating a PHP file and load the page on reboot then do its work and redirect back to such and such.

Resources