If certain commands need to be executed automatically on a Huawei device, the cron job is a helpful tool. This feature is described here with an example.
In this example, we want to shut down the GigabitEthernet0/0/4 port every day at 8pm and reactivate it at 7am in the morning. To do this, two batch files must first be created and stored in the flash:/user/bat
folder. The two batch files are structured as follows:
File shutdown.bat:
system-view
interface GigabitEthernet0/0/4
shutdown
File undoshutdown.bat:
system-view
interface GigabitEthernet0/0/4
undo shutdown
The two files can be copied to the device using SSH. How this can be done is described in a separate guide File upload to Huawei with SSH.
Both batch files should then be stored in the flash:/user/bat
folder:
<HUAWEI>cd user/bat
<HUAWEI>dir
Directory of flash:/user/bat/
Idx Attr Size(Byte) Date Time FileName
0 -rw- 35 Feb 17 2024 13:02:50 shutdown.bat
1 -rw- 35 Feb 17 2024 13:03:50 undoshutdown.bat
247,032 KB total (69,604 KB free)
Now all you have to do is execute the two batch files at the right time. To do this, two assistant tasks are created according to the following example.
<HUAWEI>system-view
Enter system view, return user view with Ctrl+Z.
[HUAWEI]assistant task SHUTDOWN
[HUAWEI-assistant-task-SHUTDOWN] if-match timer cron * * 20 * * * *
[HUAWEI-assistant-task-SHUTDOWN] perform 1 batch-file shutdown.bat
[HUAWEI-assistant-task-SHUTDOWN]quit
[HUAWEI]assistant task UNDO_SHUTDOWN
[HUAWEI-assistant-task-UNDO_SHUTDOWN] if-match timer cron * * 7 * * * *
[HUAWEI-assistant-task-UNDO_SHUTDOWN] perform 1 batch-file undoshutdown.bat
➡️ The structure of the timer is built according to the following scheme: seconds minutes hours days-of-month months days-of-week years
The tasks do not have to be started specifically, but are active straight away. With display assistant task history
you can see the history of the tasks. You can delete them again using the respective undo command.
[HUAWEI]undo assistant task SHUTDOWN
Warning: The task is running. The system may be in indeterminable state after deleting it. Continue? [Y/N]:Y
Info: Start to delete the assistant task.
[HUAWEI]undo assistant task UNDO_SHUTDOWN
Warning: The task is running. The system may be in indeterminable state after deleting it. Continue? [Y/N]:Y
Info: Start to delete the assistant task.