My cron jobs runs several times - linux

On my server i have three cron jobs:
by typing crontab -e i get the following:
0 */24 * * * wget -qO /dev/null http://www.example.com/Users/mailNotify?token=1234 >> /var/log/cronLog.txt
0 */23 * * * sh /var/www/backup/backupScript
0 */23 * * * wget -qO /dev/null http://www.example.com/Users/off_score?token=1234 >> /var/log/cronLog.txt
These cronjobs runs twice:
at 00.00 and at 01.00 every night.
The funny thing about this is that it runs all three jobs at each of the above hour.
Can anyone tell me what i have done wrong when creating these?

To have your cronjobs running once at a specific time, you shouldn't use */ as this will make your cronjobs run every 23 hours, which causes the behaviour of running at 1 and then again, 23 hours later, at 0, as cron is calculating when to run to run every 23 hours during one day.
To run all of them at midnight like you commented, use cron like this:
0 0 * * * wget -qO /dev/null http://www.example.com/Users/mailNotify?token=1234 >> /var/log/cronLog.txt
0 0 * * * sh /var/www/backup/backupScript
0 0 * * * wget -qO /dev/null http://www.example.com/Users/off_score?token=1234 >> /var/log/cronLog.txt
Cron definitions:
# * * * * * command to execute
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
You are telling cron to run every day with the 3rd * in the command.

Related

How do I delete specific history entries in Nushell?

Given a history like this in Nushell, how do I delete specific entries; e.g. entries 6, 8, and 10?
nu > history
╭────┬────────────────────╮
│ # │ command │
├────┼────────────────────┤
│ 0 │ history --clear │
│ 1 │ php --version │
│ 2 │ composer --version │
│ 3 │ node --version │
│ 4 │ npm --version │
│ 5 │ composer --version │
│ 6 │ history │
│ 7 │ php --version │
│ 8 │ history │
│ 9 │ php --version │
│ 10 │ history │
│ 11 │ composer --version │
╰────┴────────────────────╯
Based on the code that I can read here and on the documentation here, it appears that an option like this is not currently available.
However, the code indicates that the history.txt file is located at ~/.config/nushell. Utilizing this information, it is possible to accomplish what you asked by using my script below:
nushell_history_manager.py
import os
import sys
def delete_lines(file_path, line_numbers):
# open the file in read mode
with open(file_path, 'r') as f:
# read all the lines and store them in a list
lines = f.readlines()
# open the file in write mode
with open(file_path, 'w') as f:
for i, line in enumerate(lines):
# check if the current line number is not in the list of line numbers to delete
if i+1 not in line_numbers:
# if it's not, write the line to the file
f.write(line)
def print_table(file_path):
# open the file in read mode
with open(file_path, 'r') as f:
# read all the lines and store them in a list
lines = f.readlines()
# print the table header
print("╭──────┬───────────────────────────────╮")
print("│ ## │ command │")
print("├──────┼───────────────────────────────┤")
for i, line in enumerate(lines):
# print each line number and the corresponding command
print(f"│ {i+1:3} │ {line.strip():26} │")
# print the table footer
print("╰──────┴───────────────────────────────╯")
if __name__ == '__main__':
# set the file path to the history.txt file in the nushell config directory
file_path = os.path.expanduser('~/.config/nushell/history.txt')
# print the initial contents of the file in a table format
print_table(file_path)
# ask the user to enter line numbers to delete
line_numbers_str = input("Enter line numbers to delete (separated by commas): ")
# convert the entered line numbers to a list of integers
line_numbers = list(map(int, line_numbers_str.split(',')))
# delete the specified lines from the file
delete_lines(file_path, line_numbers)
# print the updated contents of the file in a table format
print_table(file_path)
Usage:
python nushell_history_manager.py
Runtime:
╭──────┬────────────────────╮
│ ## │ command │
├──────┼────────────────────┤
│ 1 │ history --clear │
│ 2 │ php --version │
│ 3 │ composer --version │
│ 4 │ node --version │
│ 5 │ npm --version │
│ 6 │ composer --version │
│ 7 │ history │
│ 8 │ php --version │
│ 9 │ history │
│ 10 │ php --version │
│ 11 │ history │
│ 12 │ composer --version │
╰──────┴────────────────────╯
Enter line numbers to delete (separated by commas): 7,9,11
╭──────┬────────────────────╮
│ ## │ command │
├──────┼────────────────────┤
│ 1 │ history --clear │
│ 2 │ php --version │
│ 3 │ composer --version │
│ 4 │ node --version │
│ 5 │ npm --version │
│ 6 │ composer --version │
│ 7 │ php --version │
│ 8 │ php --version │
│ 9 │ composer --version │
╰──────┴────────────────────╯
nu's history command does not (yet) provide a functionality to delete items similar to bash's history -d. However, you can query where the history file is located using $nu.history-path, then use drop nth to delete the lines in question.
open $nu.history-path | lines | drop nth 6 8 10 | save -f $nu.history-path
While #pmf response does EXACTLY what you ask, a more generic form is shown below. Here you don't even have to know the lines of the file that are duplicates:
export def dedupeLines [filepath: string = $"($nu.history-path)"] {
open $filepath | lines | uniq | save --force $filepath
}
In your specific case, filepath = $nu.history-path.
Executing the command below will accomplish your request
dedupeLines # since $nu.history-path is the default filepath for the command
Also for the future, Discords nushell has many questions and answers and the fellows there are extremely helpful and prompt in responding to queries. I had actually asked your question there before too.
I don't know if the python solution lines of code could be reduced, but it is interesting what nushell accomplishes in a single line when compared to the python solution.

Nodejs app not working with crontab #reboot

T'm trying to run a nodejs program after system reboot, I'm using crontab #reboot to do this but it did not work.
Here is my config steps, any idea what's wrong?
I'm using aws linux, and installed nodejs by nvm;
which node
/root/.nvm/versions/node/v8.9.3/bin/node
my test.js located
/home/ec2-user/spider/logger.js
this works fine
/root/.nvm/versions/node/v8.9.3/bin/node /home/ec2-user/spider/logger.js
this also works fine
#reboot echo "hi" > /home/reboot.txt 2>&1
crontab -e
#reboot /root/.nvm/versions/node/v7.1.0/bin/node /home/user/test.js
reboot, test.js never gets run
Also tried :
* * * * * /root/.nvm/versions/node/v8.9.3/bin/node /home/ec2-user/spider/logger.js >> /home/crontab.log 2>&1
* * * * * echo $(date '+%Y %b %d %H:%M') >> /home/reboot.txt 2>&1
* * * * * echo "hi" >> /home/hi.txt 2>&1
Only last one worked.
The crontab syntax composed of two parts, datetime to execute & command to be executed. In this case, you command is /root/.nvm/versions/node/v7.1.0/bin/node /home/user/test.js
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

Why crontab 1 0 * * * run at 12h (12/24)

I have a crontab:
1 0 * * * /opt/scripts/pg_backup mydb
I expected it run at 00:01
But it always run at 12h (12/24), not 0h every day.
How can I fix it?

Crontab and its logs

Frist time I am writing crontab.
Following is structure of time and Date
# * * * * * command to execute
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
I put entry like following
00 06 * /bin/sh /opt/cleanup.sh
Think this is not woring.
Where can I see any log of crontab??
Usually the cron commands' output is sent to the owner via mail (man mail), but this when you execute a code that returns an output (stdout and stderr). When you login you should see something like "There are new mails". I don't know though if wrong cron schedules like yours (see #fedorqui's reply) throw an error log. In any case to have the output and error of the scheduled cron jobs on file instead of on mail, you can redirect the output like this:
00 06 * * * /bin/sh /opt/cleanup.sh > /where/you/have/write/permission/cleanup.log 2>&1
If you want to append rather than overwrite, just use two >, like this:
00 06 * * * /bin/sh /opt/cleanup.sh >> /where/you/have/write/permission/cleanup.log 2>&1
To disable the log, simply schedule the following:
00 06 * * * /bin/sh /opt/cleanup.sh > /dev/null 2>&1
2>&1 means "redirect the channel 2 (error) to the pointer of channel 1 (standard output)". The standard output is redirected to the file (/my/file.sh > /my/file.log).
You need to use all the parameters. So to run every day at 6am use:
00 06 * * * /bin/sh /opt/cleanup.sh
You can see the logs in /var/log/cron, probably.
For further information, you can check the beautifully written section Debugging crontab in [crontab] tag wiki.

Cronjob every x hours between 23-16 not working

Partly using stackoverflow search I figured out how to run my cronjob every 3 hours but not between 23h-16h. That means a pause between 16h today untill 23h today. So the cronjob should start running every 3 hours at 23h and stop at 16h, then start again at 23h.
This is the result:
0 23-16/3 * * * /usr/local/bin/flexget -c /media/usb/Downloads/flexget/config.yml --cron
Now my question: Why is this not working? It does not run at all :(
I also tried:
* 23-16/3 * * * /usr/local/bin/flexget -c /media/usb/Downloads/flexget/config.yml --cron
(not sure what the difference is with 0 or with * for the minutes, rounded hours or not?)
This DOES work:
0 */3 * * * /usr/local/bin/flexget -c /media/usb/Downloads/flexget/config.yml --cron
But then it just runs every 3 hours every day, without pause between 16-23.
You can always list the hours explicitly.
0 2,5,8,11,14,23 * * * /usr/local/bin/flexget -c /media/usb/Downloads/flexget/config.yml --cron
Note this is not the same as with replacing 0 minutes with *, like:
* 2,5,8,11,14,23 * * * /usr/local/bin/flexget -c /media/usb/Downloads/flexget/config.yml --cron
The latter starts on every minute on the hours specified, i.e. 2:00, 2:01, 2:02, ..., 2:59, 5:00, 5:01, ... 5:59, ...

Resources