Autosave: 2023-01-15 16:51:55
This commit is contained in:
		
							parent
							
								
									04a97a8951
								
							
						
					
					
						commit
						6b9c94a9a5
					
				
					 1 changed files with 52 additions and 0 deletions
				
			
		
							
								
								
									
										52
									
								
								Linux/Procedural/Create_timed_systemd_job.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								Linux/Procedural/Create_timed_systemd_job.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,52 @@
 | 
				
			||||||
 | 
					---
 | 
				
			||||||
 | 
					categories:
 | 
				
			||||||
 | 
					  - Linux
 | 
				
			||||||
 | 
					tags: [systems-programming, systemd]
 | 
				
			||||||
 | 
					---
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create timed `systemd` job
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					To use systemd to run a bash script at 11am each day, you can create a new systemd service file. Here is an example of what the service file might look like:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					[Unit]
 | 
				
			||||||
 | 
					Description=Run My Script
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[Service]
 | 
				
			||||||
 | 
					Type=simple
 | 
				
			||||||
 | 
					ExecStart=/bin/bash /path/to/my_script.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[Install]
 | 
				
			||||||
 | 
					WantedBy=default.target
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Save this file to `/etc/systemd/system/my_script.service`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Then you can use systemd to schedule the script to run at 11am each day by creating a new systemd timer file. Here is an example of what the timer file might look like:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					[Unit]
 | 
				
			||||||
 | 
					Description=Run My Script Daily at 11am
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[Timer]
 | 
				
			||||||
 | 
					OnCalendar=*-*-* 11:00:00
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[Install]
 | 
				
			||||||
 | 
					WantedBy=timers.target
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Save this file to `/etc/systemd/system/my_script.timer`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Then you can enable and start the timer with the following commands:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					sudo systemctl enable my_script.timer
 | 
				
			||||||
 | 
					sudo systemctl start my_script.timer
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					This will run the script at 11am everyday
 | 
				
			||||||
 | 
					You can check the status of the timer with the following command:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					sudo systemctl status my_script.timer
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue