Archive

Articles taggués ‘shell’

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

04/04/2019 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…

Get Weather Reports from the Command Line with finger

27/03/2019 Comments off
weather-forecast-command-line-finger-graph-no2-610x433

weather-forecast-command-line-finger-graph

 

Checking weather: purpose of the command

There’s no shortage of methods to retrieve a weather report, the web is full of weather resources, everyones iPhone, Apple Watch, and smartphone has a weather app, Siri can tell you the weather, and you can even get the current weather in the menu bar of OS X or from Spotlight on the Mac too.

But for command line users, none of those options are particularly ideal, since it means leaving the command line and the task at hand.

Thanks to an interesting usage of the finger utility, you can quickly retrieve a weather report and weather forecast for virtually any city in the world, right from the command line.

With this trick you’ll see the temperature forecast (in celsius) for the day, wind direction and wind speed, precipitation and precipitation type (rain, showers, sleet, snow, etc), depth of precipitation, and more. This works with any command line that has the finger tool, whether you’re in Mac OS X, linux, BSD, Windows, it doesn’t matter, it will work the same.

To try this out yourself on the Mac, launch the OS X Terminal found in /Applications/Utilities/ and type the following command syntax:

finger (city name)@graph.no

For example, to get the weather forecast for Montreal Canada, you would use the following syntax at the command line:

finger montreal@graph.no

Lire la suite…

Categories: Système Tags: ,

Running Commands on a Remote Linux / UNIX Host

25/03/2019 Comments off

Remote Linux Commands

commands remote linuxYou would like to execute a command on a remote Linux/FreeBSD/Solaris/UNIX host and have the result displayed locally. Once result obtained it can be used by local script or program. A few examples:
=> File system and disk information

=> Get user information

=> Find out all running process

=> Find out if particular service is running or not etc

You can use rsh or ssh for this purpose. However, for security reason you should always use the ssh and NOT rsh. Please note that remote system must run the OpenSSH server.

Syntax for running command on a remote host:
ssh [USER-NAME]@[REMOTE-HOST] [command or script]

Where,

  • ssh: ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.
  • USER-NAME: Remote host user name.
  • REMOTE-HOST: Remote host ip-address or host name, such as my-site.com.
  • command or script: Command or shell script is executed on the remote host instead of a login shell.

Examples

(A) Get disk information from a server called my-site.com:
$ ssh user@my-site.com df -h

(B) List what ports are open on remote host
$ ssh user@my-site.com netstat -vatn

(C) Reboot remote host:
$ ssh root@my-site.com reboot

(D) Restart mysql server (please note enclosed multiple command line arguments using a single or double quotes)
$ ssh root@my-site.com '/etc/init.d/mysql restart'

(E) Get memory information and store result/output to local file /tmp/memory.status:
$ ssh user@my-site.com 'free -m' > /tmp/memory.status

(G) You can also run multiple command or use the pipes, following command displays memory in format of « available memory = used free memory » :
$ ssh user@debian.test.com free -m | grep "Mem:" | awk '{ print "Total memory (used free): " $3 " " $4 " = " $2 }'

See how to configure ssh for password less login using public key based authentication.

=> Related: shell script to get uptime, disk usage, cpu usage, RAM usage, system load, etc. from multiple Linux servers and output the information on a single server in a html format.

Source: NixCraft

Categories: Système Tags: , ,

nohup: How to launch standalone and independant job

17/03/2019 Comments off

Source: Wikipedia

nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.

Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.

Use

The first of the commands below starts the program abcd in the background in such a way that the subsequent logout does not stop it.

$ nohup abcd &
$ exit

Note that these methods prevent the process from being sent a ‘stop’ signal on logout, but if input/output is being received for these standard I/O files (stdin, stdout, or stderr), they will still hang the terminal.[1] See Overcoming hanging, below.

nohup is often used in combination with the nice command to run processes on a lower priority.

$ nohup nice abcd &

Existing jobs, processes

Some shells (e.g. bash) provide a shell builtin that may be used to prevent SIGHUP being sent or propagated to existing jobs, even if they were not started with nohup. In bash, this can be obtained by using disown -h job; using the same builtin without arguments removes the job from the job table, which also implies that the job will not receive the signal. Before using disown on an active job, it should be stopped by Ctrl-Z, and continued in the background by the bg command.[2] Another relevant bash option is shopt huponexit, which automatically sends the HUP signal to jobs when the shell is exiting normally.[3]

The AIX and Solaris versions of nohup have a -p option that modifies a running process to ignore future SIGHUP signals. Unlike the above-described disown builtin of bash, nohup -p accepts process IDs.[4]

Overcoming hanging

Note that nohupping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH session. A different issue that often arises in this situation is that ssh is refusing to log off (« hangs »), since it refuses to lose any data from/to the background job(s).[5][6] This problem can also be overcome by redirecting all three I/O streams:

$ nohup ./myprogram > foo.out 2> foo.err < /dev/null &

Also note that a closing SSH session does not always send a HUP signal to depending processes. Among others, this depends on whether a pseudo-terminal was allocated or not.[7]

Alternatives

  • The screen or tmux command can also detach a process from the current shell. It allows one to reattach to the process later on.
e.g. the following call will run somescript.sh detached from SIGHUP and in the background:
 $ screen -A -m -d -S somename ./somescript.sh &
  • The disown command is used to remove jobs from the job table, or to mark jobs so that a SIGHUP signal is not sent.

References

« Bug 396 – sshd orphans processes when no pty allocated ». Bugzilla.mindrot.org. Retrieved 2009-06-10.

 

Categories: Système Tags:

How to Run Cron Every 5 Minutes, Seconds, Hours, Days, Months

16/03/2019 Comments off

Source: thegeekstuff.com

Question: How do I execute certain shell script at a specific intervals in Linux using cron job? Provide examples using different time periods.

Answer: Crontab can be used to schedule a job that runs on certain internal. The example here show how to execute a backup.sh shell script using different intervals.

Also, don’t forget to read our previous crontab article that contains 15 practical examples, and also explains about @monthly, @daily, .. tags that you can use in your crontab.

1. Execute a cron job every 5 Minutes

The first field is for Minutes. If you specify * in this field, it runs every minutes. If you specify */5 in the 1st field, it runs every 5 minutes as shown below.

*/5 * * * * /home/ramesh/backup.sh

Note: In the same way, use */10 for every 10 minutes, */15 for every 15 minutes, */30 for every 30 minutes, etc.

2. Execute a cron job every 5 Hours

The second field is for hours. If you specify * in this field, it runs every hour. If you specify */5 in the 2nd field, it runs every 5 hours as shown below.

0 */5 * * * /home/ramesh/backup.sh

Note: In the same way, use */2 for every 2 hours, */3 for every 3 hours, */4 for every 4 hours, etc.

3. Execute a job every 5 Seconds

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses ‘sleep 5′ command in it.

Lire la suite…

Categories: Système Tags: , , ,