How to use shell script to execute. Net core application

Time:2021-1-16

In this article, I’ll look at how to create shell scripts to run simple. Net core applications. The reason why we have this research topic is because of our website( pzy.io )The database needs to be backed up regularly every day, and after the backup is completed, the backup files are uploaded to the remote cloud storage to ensure the data security. Because the database backup is executed by shell script, and the backup file upload function is based on. Net core development, after the backup is completed, the shell script needs to call the. Net core application.

The specific operation steps are very simple.

1. Create a basic. Net core console application, which I have built. The example is as follows:


class Program 
{ 
  /// <summary> 
  /// 
  /// </summary> 
  /// <param name="args"></param> 
  /// <returns></returns> 
  static async Task Main(string[] args) 
  { 
    var builder = new ConfigurationBuilder(); 
    builder.AddJsonFile("appsettings.json", false, false); 
    var configuration = builder.Build(); 
    var serviceProvider = new ServiceCollection() 
      .AddQiniuStorageX(configuration) 
      .BuildServiceProvider(); 
    var storageProvider = serviceProvider.GetService<IStorageProvider>(); 
    var backup = new MysqlBackQiniu(storageProvider); 
    var backupPath = configuration.GetValue<string>("BackupPath"); 
    await backup.ExecuteAsync(backupPath); 
  } 
}

2. Try compiling the project to see if the app was created successfully.

3. Based on the operating system platform version. Since I am running on Ubuntu 18.04, I execute the following command line to publish:


dotnet publish -c release -r ubuntu.18.04-x64 --self-contained

4. Create a directory on Linux (assume:/home/dotnet/test )。 Upload the file generated by point 3 publishing (under publish of the corresponding release platform) to /home/dotnet/test If the upload fails, set the directory permission to 777. After the completion of the upload, the executable file of the platform (this site is Pzy.DbBackup )The permission is set to 777.

5. Create a shell script file (assumingdbbackup.sh )。 After creation, you must change the permissions to make it an executable file, and write the following contents in the file:


#!/bin/sh 
cd /home/dotnet/test 
./Pzy.DbBackup

Here, it is important to note that. Net core application must be executed in the current directory of. Net core application, that is, first through thecd /home/dotnet/test Locate to the current directory and execute the application./Pzy.DbBackup, not directly through/home/dotnet/test/Pzy.DbBackup To execute the application, because it will cause problems such as unable to read the configuration file.

The above is how to use shell script to execute. Net core application. For more information about shell script to execute application, please pay attention to other related articles of developer!

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]