首页 > linux > BASH - 将此脚本设置为定时/例程事件的最有效方法?

BASH - 将此脚本设置为定时/例程事件的最有效方法? (BASH - Most efficient method of setting this script as a timed/routine event?)

2013-04-20 linuxbashgnu

问题

我最近开始了一个简单的项目,只是为了帮助我更好地学习BASH脚本语言。无论如何,此脚本设置为以给定间隔交替/旋转用户的桌面背景/墙纸。

鉴于此任务很可能每30分钟到1小时完成一次,我该怎么做呢。30分钟/ 1小时计时器效率非常低吗?或者,cronjobs可以为我做得更好吗?

另外,我怎么能让这个脚本在后台运行,这样终端窗口并不总是需要打开?

如果可以的话,你能否在语法中提供某种想法。

解决方法

这将是一个合适的工作croncron将负责定期调用脚本。然后,当脚本运行并管理在后台运行的脚本时,您不必担心脚本。

在后台运行将是非常有用的,因为脚本不需要做太多 - 只需更改当前的桌面设置。通常,脚本只需要一小段时间即可完成任务。

cron 条目有六个字段 - :

 mins hours day  month day-of-week path_to_command
 0-59 0-23  1-31 1-12      0-6      command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.

cron 所有日期和月份每小时运行脚本的条目 - :

0 * * * * /path/change_wallpaper.sh

列出您当前的cron工作,输入

 crontab -l 

编辑您的cron作业并添加新的cron条目 - :

 crontab -e

检查新设置是否到位 - :

 crontab -l

问题

I've recently started a simple project, just to help me learn the BASH scripting language a little better. Anyway, this script is set to alternate/rotate the user's desktop background/wallpaper at a given interval.

Given that this task would most likely be done every 30 minutes to 1 hour, how should I go about doing this. Would 30 minute/1 hour timers be very inefficient? Or, could cronjobs do a better job for me?

Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?

Could you provide some sort of an idea into the syntax, if you can, as well.

解决方法

This would be a suitable job for cron. cron would take care of invoking the script at regular intervals. You would not then have to be concerned in your script when the script should run and managing a script running in the background.

Running in the background would be extravagent as the script does not need to do much - not much more than change the current desktop setting. Typically the script would only take a small fraction of a second to complete the task.

cron entries have six fields-:

 mins hours day  month day-of-week path_to_command
 0-59 0-23  1-31 1-12      0-6      command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.

cron entry to run the script every hour for all days and months-:

0 * * * * /path/change_wallpaper.sh

to list your current cron jobs, type

 crontab -l 

Edit your cron jobs and add the new cron entry-:

 crontab -e

Check the new setting is in place -:

 crontab -l
相似信息