Archive

Articles taggués ‘crontab’

How to list the crontabs for all users?

12/05/2023 Comments off

I ended up writing a script (I’m trying to teach myself the finer points of bash scripting, so that’s why you don’t see something like Perl here). It’s not exactly a simple affair, but it does most of what I need. It uses Kyle’s suggestion for looking up individual users’ crontabs, but also deals with /etc/crontab(including the scripts launched by run-parts in /etc/cron.hourly/etc/cron.daily, etc.) and the jobs in the /etc/cron.d directory.source: How do I list all cron jobs for all users?

I ended up writing a script (I’m trying to teach myself the finer points of bash scripting, so that’s why you don’t see something like Perl here). It’s not exactly a simple affair, but it does most of what I need. It uses Kyle’s suggestion for looking up individual users’ crontabs, but also deals with /etc/crontab(including the scripts launched by run-parts in /etc/cron.hourly/etc/cron.daily, etc.) and the jobs in the /etc/cron.d directory.

It takes all of those and merges them into a display something like the following:

#!/bin/bash
# System-wide crontab file and cron job directory. Change these for your system.
 CRONTAB='/etc/crontab'
 CRONDIR='/etc/cron.d'
# Single tab character. Annoyingly necessary.
 tab=$(echo -en "\t")
# Given a stream of crontab lines, exclude non-cron job lines, replace
 # whitespace characters with a single space, and remove any spaces from the
 # beginning of each line.
 function clean_cron_lines() {
 while read line ; do
 echo "${line}" |
 egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' |
 sed --regexp-extended "s/\s+/ /g" |
 sed --regexp-extended "s/^ //"
 done;
 }
# Given a stream of cleaned crontab lines, echo any that don't include the
 # run-parts command, and for those that do, show each job file in the run-parts
 # directory as if it were scheduled explicitly.
 function lookup_run_parts() {
 while read line ; do
 match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+')
if [[ -z "${match}" ]] ; then
 echo "${line}"
 else
 cron_fields=$(echo "${line}" | cut -f1-6 -d' ')
 cron_job_dir=$(echo "${match}" | awk '{print $NF}')
if [[ -d "${cron_job_dir}" ]] ; then
 for cron_job_file in "${cron_job_dir}"/* ; do # */
 [[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}"
 done
 fi
 fi
 done;
 }
# Temporary file for crontab lines.
 temp=$(mktemp) || exit 1
# Add all of the jobs from the system-wide crontab file.
 cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}"
# Add all of the jobs from the system-wide cron directory.
 cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}" # */
# Add each user's crontab (if it exists). Insert the user's name between the
 # five time fields and the command.
 while read user ; do
 crontab -l -u "${user}" 2>/dev/null |
 clean_cron_lines |
 sed --regexp-extended "s/^((\S+ +){5})(.+)$/\1${user} \3/" >>"${temp}"
done <
# Output the collected crontab lines. Replace the single spaces between the
 # fields with tab characters, sort the lines by hour and minute, insert the
 # header line, and format the results as a table.
 cat "${temp}" |
 sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" |
 sort --numeric-sort --field-separator="${tab}" --key=2,1 |
 sed "1i\mi\th\td\tm\tw\tuser\tcommand" |
 column -s"${tab}" -t
rm --force "${temp}"

source: How do I list all cron jobs for all users?

Change & Set the Default crontab Editor

11/05/2023 Comments off


Most hardcore command line users and unix geeks love vi, but I prefer nano. If you want to change your default crontab editor to nano, here’s how to do this:

For a one time edit, launch the terminal and type:

EDITOR=nano crontab -e

If you want to set nano as your default editor in general, you use this command:

export EDITOR=nano

Now when you go to edit crontab, nano will be the default editor than vi. You can test this by typing:

crontab -e

Looking beyond Mac OS X, this should work in Linux as well.

Categories: Système Tags: , , , ,

Scripts shell de sauvegarde

17/04/2023 Comments off

Une des façons les plus simples de sauvegarder un système utilise un script shell. Par exemple, un script peut être utilisé pour configurer les répertoires à sauvegarder et transmettre ces répertoires comme arguments à l’utilitaire tar, ce qui crée un fichier d’archive. Le fichier d’archive peut ensuite être déplacé ou copié dans un autre emplacement. L’archive peut également être créée sur un système de fichiers distant tel qu’un montage NFS.

L’utilitaire tar crée un fichier d’archive de plusieurs fichiers ou répertoires. tar peut également filtrer les fichiers par le biais des utilitaires de compression, réduisant ainsi la taille du fichier d’archive.

Categories: Système Tags: , , ,

Dumper une base MySQL avec horodatage dans le nom du fichier

16/04/2023 Comments off

J’ai un projet qui utilise la base de données MySQL. Je souhaite sauvegarder la base de données tous les jours, donc j’utilise ceci:

mysqldump -h host -u user -p database de mots de passe> 'location.sql'

Je souhaite que les fichiers soient nommés avec l’horodatage, c’est-à-dire:

Aujourd’hui, le fichier sera nommé quelque chose-07-05-2014 08-00-00
Demain sera quelque chose-08-05-2014 08-00-00

Comment ajouter un timestamp formaté avec le nom de fichier exporté?

Réponse:

Une solution serait:

mysqldump -h host -u user -p password database > quelque chose-$(date +%d-%m-%Y %H %M %S).sql

Pour un horodatée qui permette le tri correct des fichiers, il fait changer l’ordre des paramètres et utiliser:

%Y-%m-%d

de manière à trier sur année, mois puis jour. Ne rien changer pour les hh:mm:ss puisque le tri se fair naturellement dans cas.

Pour automatiser ce dump, il faut insérer cette commande dans le crontab (du root ou de l’utilisateur):

0 */8 * * * root /usr/bin/mysqldump -h host -u user -p password database > quelque chose-$(date +%d-%m-%Y %H %M %S).sql

pour que la commande s’exécute toutes les 3 heures (24h ÷ 8).

Categories: Système Tags: , , ,

inotify / incron : Lancer une commande en cas d’action sur un fichier/un répertoire

19/02/2023 Comments off

inotify”, Remplaçant de “dnotify”, est une technologie, intégrée au noyau Linux (>=2.6.13) , destinée à notifier les événements, modifications, accès, etc, effectués sur le contenu d’un système de fichiers en ce basant sur le contrôle des “inodes” (structures de données contenant des informations sur les fichiers d’un systèmes de fichiers).

« incron« , Pour « INotify CRON », permet d’exploiter les informations « d’inotify » afin d’effectuer une action, commande(s), scripts, etc, en cas de modifications de fichiers ou de répertoires donnés.

Installation de inotify

« inotify » Est intégré au noyau et est activé dans les kernels fournis par les distributions.

« incron » Est, de son côté, empaqueté sur la plupart des distributions mais nécessitera, sur Red Hat et ses dérivées (CentOS, Scientific Linux, etc) l’ajout des miroirs EPEL (« Extra Packages for Enterprise Linux » voir « Ajout des miroirs EPEL (Extra Packages for Enterprise Linux) sous Red Hat like (CentOS, RHEL, SL, …) » sur Admin Linux). L’installation s’effectue via le gestionnaire de paquet de votre distribution.

Sous Ubuntu, Debian et ses dérivés :

# apt-get install incron

Sous les dérivés de Red Hat :

# yum install incron

Sous Gentoo Linux « emerge » se chargera de l’installation tous comme « pacman » le fera très bien sous Arch Linux.

Lire la suite…