Crontab and its logs - cron

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.

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.

How to extract only required data from console output of linux commands

After executing the given command mc ilm rule ls myminio/receive i get the below output..
┌───────────────────────────────────────────────────────────────────────────────────────┐
│ Expiration for latest version (Expiration) │
├──────────────────────┬─────────┬────────┬──────┬────────────────┬─────────────────────┤
│ ID │ STATUS │ PREFIX │ TAGS │ DAYS TO EXPIRE │ EXPIRE DELETEMARKER │
├──────────────────────┼─────────┼────────┼──────┼────────────────┼─────────────────────┤
│ cf59qti8ufll86j92g │ Enabled │ - │ - │ 90 │ false │
│ cf7701klgt9a0vts40 │ Enabled │ - │ - │ 90 │ false │
│ cf7712tlgie49qo4p0 │ Enabled │ - │ - │ 90 │ false │
From which i want only the list of ID from this output how do i extract it..
My excepted output :
cf59qti8ufll86j92g
cf7701klgt9a0vts40
cf7712tlgie49qo4p0
For example,
command | perl -ne 'print if (s/^\s*\S+\s+([a-z0-9]{18})\s.*$/\1/)'

Crontab doesn't execute the .sh files

I recently discover crontab on my raspberry pi and I wanted to try it so I made different lignes for execute simples programs:
$
0 21 * * * /home/pi/Desktop/Programmation/Batch/msg.sh
30 21 * * * /home/pi/Desktop/Programmation/Batch/msg2.sh
0 22 * * * /home/pi/Desktop/Programmation/Batch/msg3.sh
15 22 * * * /home/pi/Desktop/Programmation/Batch/msg4.sh
30 22 * * * /home/pi/Desktop/Programmation/Batch/msg5.sh
35 22 * * * /home/pi/Desktop/Programmation/Batch/msg6.sh
(I know: it's not batch but sh !!)
Here is the .sh program (there are all the sames):
#!/bin/bash
echo -e "Hello World !"
sleep 1000000000000000000
But that's doesn't work !!
I look on internet and I tried to change the TZ of crontab, I tried the command timedatectl to verify mi date settings :
Local time: sam. 2020-10-10 18:23:37 CEST
Universal time: sam. 2020-10-10 16:23:37 UTC
RTC time: n/a
Time zone: Europe/Paris (CEST, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
And That doesn't work... So please help me !! Thank you very much !!!

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)

My cron jobs runs several times

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.

Resources