Run command every second in Bash? Run command every second in Bash? bash bash

Run command every second in Bash?


If you want to run a command at one second intervals (one second between the end of one command and the beginning of the next, which is not the same as running every second), just do:

while sleep 1; do cmd; done

If you want that to start on reboot, the method will depend on your system.

Note that it is certainly possible to start an execution every second rather than running at one second intervals, but I suspect that is not actually what you want. In addition, there are inherent risks with doing so. For example, if the system gets sluggish and the command starts taking longer than one second to run you may run out of resources.


The command watch will do this for you straight up. It also displays the result in a nice way.

$ watch -n 1 date

Substitute date for your command. The -n option specifies the interval in seconds.


To add my two cents to this... If the cron's one minute interval is too long for you, you can take advantage of the systemd's capability to restart services repeatedly.

[Unit]Description=Poll something each second[Service]Type=simpleExecStart=/opt/poller/poll.shRestart=alwaysRestartSec=1StartLimitInterval=0[Install]WantedBy=multi-user.target

I know it's a messy and sort of "never ever do this" approach. But it works perfectly an is fairly simple to set up.